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.

duration=shortest

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

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).

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.