In this article, we will discuss how to replace soundtracks in an MP4 video using FFmpeg, a powerful multimedia framework. This process involves concatenating two audio files and embedding them into the video.
Prerequisites
Before we begin, ensure you have the following:
- FFmpeg installed on your system
- Three files: an MP4 video (let's say "vid.mp4"), and two audio files ("1.aac" and "2.aac")
Step 1: Extract the Original Audio Streams
First, we need to extract the original audio streams from the MP4 video and save them as separate files. Use the following command:
ffmpeg -i vid.mp4 -vn -map 0:a:0 output1.aac
This command extracts the first audio stream ("1.aac") from the video file "vid.mp4". The "-vn" flag tells FFmpeg not to process the video stream.
Repeat the same process to extract the second audio stream:
ffmpeg -i vid.mp4 -vn -map 0:a:1 output2.aac
Step 2: Concatenate the Audio Files
Now, we will concatenate the two audio files into a single file using FFmpeg:
ffmpeg -i "concat:file1.txt" -c:a aac -ar 44100 -ac 2 output.aac
Create a file named "file1.txt" with the following content:
file 'output1.aac'
file 'output2.aac'
This command concatenates the two audio files and saves the result as "output.aac".
Step 3: Replace the Original Soundtrack with the Concatenated Audio
Finally, we will replace the original soundtrack in the MP4 video with the concatenated audio:
ffmpeg -i vid.mp4 -vcodec copy -ac:a aac -ar 44100 -ac 2 -i output.aac -map 0:v -map 1:a out.mp4
This command creates a new MP4 file named "out.mp4" with the concatenated audio as its soundtrack.
In this article, we learned how to replace the soundtrack in an MP4 video using FFmpeg by concatenating two audio files and embedding them into the video.