Concatenate Audio with Different Sample Rates in FFmpeg

Concatenating audio files with varying sample rates in FFmpeg causes errors or distorted playback if combined directly. To successfully join these files using FFmpeg's concat filter, you must normalize the audio streams to a common sample rate and channel layout before passing them into the concatenation stage. This guide demonstrates how to achieve this seamlessly using the -filter_complex option.

Why Direct Concatenation Fails

The FFmpeg concat filter requires all input streams to share identical audio parameters, including sample rate, channel layout, and sample format. If you pass streams with mismatched properties (for example, one file at 44,100 Hz and another at 48,000 Hz), FFmpeg will either terminate with an error or produce an output with pitch shifts, audio drift, or clicks.

The Solution: Using aresample or aformat

To fix sample rate mismatches, insert a resampling filter before the concat filter inside a single FFmpeg filtergraph. The aformat filter is ideal because it allows you to standardize both the sample rate and the channel layout simultaneously.

Command Example

Here is the command to concatenate two files with different sample rates into a single 48,000 Hz stereo output:

ffmpeg -i input1.wav -i input2.mp3 -filter_complex \
"[0:a]aformat=sample_rates=48000:channel_layouts=stereo[a0]; \
 [1:a]aformat=sample_rates=48000:channel_layouts=stereo[a1]; \
 [a0][a1]concat=n=2:v=0:a=1[outa]" \
-map "[outa]" output.mp3

Command Breakdown

  • -i input1.wav -i input2.mp3: Specifies the source audio files.
  • -filter_complex: Initiates a multi-step filtergraph.
  • [0:a]aformat=...[a0]: Takes the audio stream from the first input (0:a), resamples it to 48000 Hz, converts it to stereo, and assigns the result to a temporary label named [a0].
  • [1:a]aformat=...[a1]: Applies the identical conversion to the second input (1:a) and labels it [a1].
  • [a0][a1]concat=n=2:v=0:a=1[outa]: Takes the two normalized streams, specifies n=2 inputs, disables video concatenation (v=0), enables audio concatenation (a=1), and labels the output [outa].
  • -map "[outa]": Directs FFmpeg to write the processed stream from [outa] into the destination file.

Concatenating More Than Two Files

If you need to merge three or more files, expand the filtergraph by adding an aformat filter for each input and updating the input count in concat:

ffmpeg -i input1.wav -i input2.mp3 -i input3.aac -filter_complex \
"[0:a]aformat=sample_rates=48000:channel_layouts=stereo[a0]; \
 [1:a]aformat=sample_rates=48000:channel_layouts=stereo[a1]; \
 [2:a]aformat=sample_rates=48000:channel_layouts=stereo[a2]; \
 [a0][a1][a2]concat=n=3:v=0:a=1[outa]" \
-map "[outa]" output.wav