Configure FFmpeg amix Output Duration

When mixing multiple audio streams using the FFmpeg amix filter, controlling the length of the final output is crucial to prevent unwanted silence or abrupt cutoffs. This guide explains how to use the duration parameter within the amix filter to determine whether the mixed output ends when the longest, shortest, or first input track finishes playing.

The amix filter provides a specific option called duration to manage the final output length. By default, FFmpeg continues mixing until the longest input file ends, which may result in silence if one track is much shorter than the other.

To configure this behavior, you can set the duration option to one of three values: longest, shortest, or first.

Option 1: shortest

Setting duration=shortest stops the mixing process and ends the output file as soon as the shortest input track finishes. This is ideal when you want to overlay a short sound effect or voiceover and have the entire output stop when that short audio ends.

ffmpeg -i main_music.mp3 -i voiceover.mp3 -filter_complex "amix=inputs=2:duration=shortest" output.mp3

Option 2: first

Setting duration=first binds the duration of the output file directly to the length of the very first input specified in your command (input index 0). The duration of any subsequent inputs will be ignored.

ffmpeg -i main_music.mp3 -i sound_effect.mp3 -filter_complex "amix=inputs=2:duration=first" output.mp3

In this example, the output will be exactly the same length as main_music.mp3.

Option 3: longest (Default)

Setting duration=longest (or omitting the option entirely) ensures that the output file keeps playing until all input streams have finished.

ffmpeg -i track1.mp3 -i track2.mp3 -filter_complex "amix=inputs=2:duration=longest" output.mp3

Managing Volume Dropouts

When using duration=longest, you may notice a sudden volume increase in the remaining stream when one stream ends. This is because FFmpeg automatically balances the volume based on the number of active streams. To control how smoothly the volume transitions when a stream drops out, you can use the dropout_transition option (set in seconds):

ffmpeg -i track1.mp3 -i track2.mp3 -filter_complex "amix=inputs=2:duration=longest:dropout_transition=3" output.mp3

This forces a 3-second fade transition for the volume readjustment instead of an instant jump.