Merge Two Audio Tracks into Video File with a Single FFmpeg Operation
If you have a video file and want to add two additional audio tracks to it using a single FFmpeg operation, this article will guide you through the process. This technique can be useful for creating a video file with multiple audio tracks, such as separate audio tracks for different languages or commentary.
Prerequisites
Before we begin, ensure you have the following:
- A video file (with or without an audio track)
- Two separate audio files
- The FFmpeg tool installed on your system
Basic FFmpeg Operation
To start, open a terminal or command prompt and navigate to the directory containing the video and audio files.
The basic FFmpeg command to merge audio into a video file is as follows:
ffmpeg -i input.mp4 -i input.mp3 -c:v copy -c:a aac output.mp4
In this command:
-i input.mp4: specifies the input video file-i input.mp3: specifies the input audio file-c:v copy: copies the video stream without re-encoding-c:a aac: encodes the audio to AAC formatoutput.mp4: specifies the output file
Merging Two Audio Tracks
To merge two audio tracks into a video file, simply add another audio input and output stream to the FFmpeg command:
ffmpeg -i input.mp4 -i audio1.mp3 -i audio2.mp3 -map 0:v -map 1:a -map 2:a -c:v copy -c:a:0 aac -c:a:1 copy output.mp4
In this command:
-map 0:v: selects the video stream from the first input-map 1:a: selects the first audio stream from the second input-map 2:a: selects the second audio stream from the third input-c:a:0 aac: encodes the first audio stream to AAC format-c:a:1 copy: copies the second audio stream without re-encoding
With a single FFmpeg operation, you can merge two audio tracks into a video file while preserving the original video stream. This technique can be useful for creating multilingual or multitrack video files.
References
-
FFmpeg Documentation: https://ffmpeg.org/documentation.html
-
FFmpeg Map Option: https://ffmpeg.org/ffmpeg-filters.html# map-1