FFmpeg H264 MP4 with Dual AAC and AC3 Audio

This article provides a quick guide on how to use FFmpeg to create an MP4 file containing one H.264 video track and two distinct audio tracks: a primary AAC track and a secondary AC-3 track. You will learn the exact command-line syntax required to map the inputs, select the codecs, configure the bitrates, and output a highly compatible multi-track media file.

The FFmpeg Command

To generate an MP4 container with an H.264 video track, a primary AAC audio track, and a secondary AC-3 track, use the following FFmpeg command. This example assumes you are starting with a single input file (input.mkv) that contains both video and at least one audio stream.

ffmpeg -i input.mkv \
  -map 0:v:0 \
  -map 0:a:0 \
  -map 0:a:0 \
  -c:v libx264 -pix_fmt yuv420p \
  -c:a:0 aac -b:a:0 192k \
  -c:a:1 ac3 -b:a:1 448k \
  output.mp4

Command Breakdown

Understanding how FFmpeg maps and encodes these streams is key to customizing the output to your specific needs:

Stream Copying (Without Re-encoding Video)

If your input file already contains an H.264 encoded video stream, you can skip the video encoding process to save time and preserve original quality. To do this, replace -c:v libx264 with -c:v copy:

ffmpeg -i input.mkv \
  -map 0:v:0 \
  -map 0:a:0 \
  -map 0:a:0 \
  -c:v copy \
  -c:a:0 aac -b:a:0 192k \
  -c:a:1 ac3 -b:a:1 448k \
  output.mp4

Setting Track Metadata and Default Status

To ensure media players recognize the primary and secondary purposes of the audio tracks, you can set track titles and specify which track should play by default:

ffmpeg -i input.mkv \
  -map 0:v:0 \
  -map 0:a:0 \
  -map 0:a:0 \
  -c:v libx264 \
  -c:a:0 aac -b:a:0 192k -metadata:s:a:0 title="Stereo (AAC)" \
  -c:a:1 ac3 -b:a:1 448k -metadata:s:a:1 title="Surround (AC3)" \
  -disposition:a:0 default \
  -disposition:a:1 0 \
  output.mp4

In this command, -disposition:a:0 default forces media players to load the AAC track first, while -disposition:a:1 0 ensures the AC-3 track remains an optional secondary selection.