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:

  • -i input.mkv: Specifies the source file containing the video and audio tracks.
  • -map 0:v:0: Selects the first video stream from the first input file (index 0) to be written to the output.
  • -map 0:a:0 (First instance): Maps the first audio stream from the input to become the first audio track (a:0) in the output file.
  • -map 0:a:0 (Second instance): Maps the same source audio stream a second time to become the second audio track (a:1) in the output file.
  • -c:v libx264: Encodes the video stream to H.264 using the libx264 library.
  • -pix_fmt yuv420p: Ensures the pixel format is YUV 4:2:0, which is required for maximum compatibility across older hardware players and browsers.
  • -c:a:0 aac -b:a:0 192k: Encodes the first output audio track using the AAC codec with a bitrate of 192 kbps. This serves as the primary, highly compatible stereo or surround track.
  • -c:a:1 ac3 -b:a:1 448k: Encodes the second output audio track using the AC-3 (Dolby Digital) codec with a bitrate of 448 kbps. This serves as the secondary track, ideal for home theater receivers.

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.