Combining Audio Tracks in a Video With Ffmpeg

I use OBS Studio to store replays of my games with my friends for later. OBS writes two audio tracks to the file, one for game audio and one for my mic. I was surprised to find that multi-track files were not properly supported by many programs like Handbrake (or YouTube for that matter), so I needed to combine the audio tracks.

This is just a quick post to store the command I used to do that. This command merges the audio tracks in the input video, lowers the volume on the mic track (track 1), and copies the video data without re-encoding:

ffmpeg -i 'input.mkv' -filter_complex '[0:a:1]volume=0.1[l];[0:a:0][l]amerge=inputs=2[a]' -map '0:v:0' -map '[a]' -c:v copy -c:a libmp3lame -q:a 3 -ac 2 'output.mp4'

My understanding of the command is as follows:

  • filter_complex is a series of filters that operates on the input. Inside it, filters have inputs and outputs that are the parts in square brackets. So a filter with two inputs looks like this: [input0][input1]filter[output].
  • [0:a:1] picks the 0th input file’s 1st audio track as input for the filter.
  • volume=0.1[l] lowers the volume (this can also use dB units) and puts the result into l.
  • [0:a:0][l] selects both the 0th input file’s 0th audio track and l from the previous filter as inputs.
  • amerge=inputs=2 merges the given 2 input audio tracks. Here you would need to modify the 2 and the inputs if you had more than 2 audio tracks to merge.
  • [a] puts the output of the amerge into a.
  • map switches select the used video and audio tracks for the output. Here we use the original video as video track and the filtered audio a as the audio track.
  • c:v copy sets video codec to copy which just copies it without changing.
  • c:a libmp3lame uses LAME to convert the audio to MP3, here you could use some other codec if you wanted.
  • q:a 3 is VBR quality 3 for the LAME codec.
  • ac 2 sets output audio to have 2 channels (stereo).

I’m still a beginner in this, so if you do notice any errors, please comment. Documenting this is both a note for myself and hopefully may help others find this by googling (it was surprisingly difficult to find this combination).

Related Stack Overflow post: