Add sound tracks to a video file with FFmpeg

Short tutorial how to extract & merge audio tracks from/to video files with FFmpeg.

Extracting audio track(s) from video file

Say, we have a video file and we need to extract specific audio track from it. First, let's see which audio tracks we have in this file. Run the following command:

ffmpeg -i <videofile>

Look at the output and find strings starting with "Stream #":

    Stream #0:0: Video: mpeg4 (Advanced Simple Profile) (XVID / 0x44495658), yuv420p, 720x304 [SAR 1:1 DAR 45:19], 23.98 tbr, 23.98 tbn, 23.98 tbc
    Stream #0:1: Audio: ac3 ([0] [0][0] / 0x2000), 48000 Hz, 5.1(side), s16, 448 kb/s

Let's extract audio track (stream 0:1):

ffmpeg -i <videofile> -map 0:1 -vn -acodec copy output.ac3

Adding multiple audio tracks to existing video file

Say, we have existing video file with single audio track, and we want to merge two more audio tracks to this video file.

Here is final command:

ffmpeg -i <sourceVideoFile> -i <sourceAudioFile1> -i <sourceAudioFile2> -map 0:0 -map 0:1 -map 1:0 -map 2:0 -c:v copy -c:a copy <outputVideoFile>

Let's see what's happening here.

  • first we pass 3 source files, each with -i
  • then comes 4 -map parameters. Each consecutive map parameter adds another stream to output file. After -map we specify from where to take the stream. First number means number of input file (which we specified earlier with -i), and second number means number of stream inside this input file. So, here we tell exactly the following (remember files and streams are indexed from zero):
    • map 0:0: copy stream 0 from first file (this is video stream) to stream 0 of output file
    • map 0:1: copy stream 1 from first file (this is audio stream) to stream 1 of output file
    • map 1:0: copy stream 0 from second file to stream 2 of output file
    • map 2:0: copy stream 0 from third file to stream 3 of output file
  • c:v copy means "Copy all video streams without re-encoding"
  • c:a copy means "Copy all audio streams without re-encoding"

That's it!