FFmpeg Set Primary and Fallback Audio Tracks in MP4
This article explains how to configure primary and fallback relations for multiple audio tracks in an MP4 container using FFmpeg. You will learn how to use mapping and disposition flags to define which audio track a media player should play by default (the primary track) and which tracks should serve as alternative or fallback options.
When an MP4 container contains multiple audio tracks—such as
different languages or stereo and surround sound configurations—media
players rely on specific container metadata to decide which track to
play automatically. In FFmpeg, you configure this relationship using the
-disposition flag, which sets the default status of each
stream.
Step 1: Map the Video and Audio Streams
To manage multiple audio tracks, you must first explicitly map the
video stream and all audio streams using the -map option.
This ensures FFmpeg keeps all the desired tracks in the output file.
ffmpeg -i input.mp4 -map 0:v:0 -map 0:a:0 -map 0:a:1 -c copy output.mp4In this command: * -map 0:v:0 selects the first video
stream. * -map 0:a:0 selects the first audio stream. *
-map 0:a:1 selects the second audio stream. *
-c copy copies the codecs without re-encoding, preserving
quality and saving time.
Step 2: Configure the Primary and Fallback Dispositions
To set the primary (default) track and mark the others as fallbacks,
use the -disposition flag. You apply this flag specifically
to the audio streams (a:0 for the first audio stream,
a:1 for the second, and so on).
Run the following command to set the first audio track as the primary default and the second track as the fallback:
ffmpeg -i input.mp4 -map 0:v:0 -map 0:a:0 -map 0:a:1 -c copy -disposition:a:0 default -disposition:a:1 0 output.mp4How the Dispositions Work
-disposition:a:0 default: This marks the first audio stream as the default stream. Compliant media players will automatically select this track for playback.-disposition:a:1 0: Setting the value to0clears all disposition flags (including “default”) from the second audio stream. This designates it as a non-default, fallback track that the user can manually switch to in their player.
Configuring Language Tags for Better Fallback Behavior
Media players often use language settings to choose fallback tracks.
To ensure players handle your primary and fallback tracks correctly
based on user preferences, you should define the language metadata for
each track using the -metadata:s:a option:
ffmpeg -i input.mp4 -map 0:v:0 -map 0:a:0 -map 0:a:1 -c copy -disposition:a:0 default -disposition:a:1 0 -metadata:s:a:0 language=eng -metadata:s:a:1 language=spa output.mp4In this configuration, an English speaker’s player will default to
the primary English track (a:0), while a Spanish speaker’s
player may automatically fall back to the Spanish track
(a:1) despite the default flag on the English track.