FFmpeg amix duration: Handling different input lengths

When mixing multiple audio streams in FFmpeg using the amix filter, handling tracks of varying lengths is a common challenge. This article explains how the duration parameter controls the length of the final output, detailing the behavior of the longest, shortest, and first options to help you achieve precise audio mixing results.

The amix filter in FFmpeg merges multiple audio inputs into a single output. Because these inputs often have different durations, FFmpeg provides the duration parameter to define when the mixing process should stop. You can configure this parameter with one of three values: longest, shortest, or first.

duration=longest (Default)

When set to longest, the output duration matches the length of the longest input file.

  • Behavior: Mixing continues until every single input stream has finished playing.

  • Handling Shorter Inputs: Once a shorter audio track reaches its end, it ceases to output sound, and FFmpeg pads that specific stream with silence. The remaining longer tracks continue to play normally until the longest one finishes.

  • Example Command:

    ffmpeg -i input1.mp3 -i input2.mp3 -filter_complex "amix=inputs=2:duration=longest" output.mp3

duration=shortest

When set to shortest, the output duration matches the length of the shortest input file.

  • Behavior: The mixing process terminates immediately when the first input stream ends.

  • Handling Longer Inputs: Any audio remaining in the longer tracks is abruptly cut off. This option is ideal when you want to ensure no silence or trailing background music remains after your primary, shorter audio track ends.

  • Example Command:

    ffmpeg -i input1.mp3 -i input2.mp3 -filter_complex "amix=inputs=2:duration=shortest" output.mp3

duration=first

When set to first, the output duration is dictated entirely by the very first input stream specified in the command line (index 0).

  • Behavior: The mix ends the moment the first input finishes, regardless of whether the other inputs are longer or shorter.

  • Handling Other Inputs: If the first input is shorter than the others, the output cuts off early. If the first input is longer, the other streams will stop playing when they end, and the first stream will continue playing until its completion.

  • Example Command:

    ffmpeg -i primary_voice.mp3 -i background_music.mp3 -filter_complex "amix=inputs=2:duration=first" output.mp3

Volume Adjustment with dropout_transition

When inputs of different lengths end during an amix operation, FFmpeg automatically adjusts the volume of the remaining streams to prevent sudden volume drops. By default, the dropout_transition parameter is set to 2 seconds, which is the time over which the volume of the remaining streams is re-normalized. If you want an abrupt transition without volume scaling, you can set dropout_transition=0.