FFmpeg amerge with Different Length Inputs

This article explains how to use the FFmpeg amerge filter when your input audio streams have different durations. You will learn how the default truncation behavior works and how to use the apad filter alongside the -shortest flag to pad shorter inputs with silence, ensuring the final output matches the duration of your longest input stream.

The Default Behavior: Shortest Input Rules

By default, the amerge filter merges multiple audio streams into a single multi-channel stream, but it stops processing as soon as the shortest input stream ends.

For example, if you run this basic command:

ffmpeg -i long.mp3 -i short.mp3 -filter_complex "amerge=inputs=2" output.m4a

The output file output.m4a will cut off early, matching the exact duration of short.mp3.

The Solution: Pad with Silence to Match the Longest Input

To keep the entire duration of the longest input stream, you must pad the shorter stream with infinite silence using the apad filter, and then tell FFmpeg to stop rendering when the non-padded stream ends using the -shortest flag.

Here is the command to achieve this:

ffmpeg -i long.mp3 -i short.mp3 -filter_complex "[1:a]apad[padded];[0:a][padded]amerge=inputs=2" -shortest output.m4a

How This Command Works:

  1. [1:a]apad[padded]: This takes the second input (short.mp3 at index 1) and applies the apad filter, which appends an infinite stream of silence to the end of it. The output of this filter is labeled [padded].
  2. [0:a][padded]amerge=inputs=2: This merges the first input (long.mp3 at index 0) and the newly created [padded] stream into a multi-channel layout.
  3. -shortest: Because the padded stream is now infinitely long, you must include the -shortest global flag. This instructs FFmpeg to stop writing to the output file as soon as the longest actual input file (long.mp3) finishes.