FFmpeg Concat Files with Different Sample Rates
This article explains how to merge audio or video files with
different sample rates using FFmpeg’s concat filter.
Because the concat filter requires all input streams to
have identical parameters, attempting to merge files with mismatching
sample rates directly will result in errors or corrupted playback. To
resolve this, you must use the aresample filter to
normalize the sample rates of all inputs within a filtergraph before
concatenating them.
The Problem with Direct Concatenation
When using the FFmpeg concat demuxer or the
concat filter, the inputs must share the same codec
parameters, including channel layout and sample rate. If you try to
merge a file with a 44,100 Hz sample rate with one that has a 48,000 Hz
sample rate, FFmpeg will likely throw an error or output a file with
distorted, sped-up, or slowed-down audio.
To bypass this limitation, you must resample the audio streams to a
common sample rate before passing them to the concat
filter.
How to Resample and Concatenate Using Filter Complex
The most efficient way to handle this on the fly is by using
-filter_complex. This allows you to chain the
aresample filter to each input stream, normalizing them to
a target sample rate (such as 48,000 Hz) before piping them into the
concat filter.
Here is the template command to merge two audio files with different sample rates:
ffmpeg -i input1.mp3 -i input2.mp3 -filter_complex \
"[0:a]aresample=48000[a1]; \
[1:a]aresample=48000[a2]; \
[a1][a2]concat=n=2:v=0:a=1[outa]" \
-map "[outa]" output.mp3Command Breakdown:
-i input1.mp3 -i input2.mp3: Specifies the two input files.[0:a]aresample=48000[a1]: Takes the audio stream of the first input (0:a), resamples it to 48,000 Hz, and labels the temporary output as[a1].[1:a]aresample=48000[a2]: Takes the audio stream of the second input (1:a), resamples it to 48,000 Hz, and labels the temporary output as[a2].[a1][a2]concat=n=2:v=0:a=1[outa]: Takes the resampled streams[a1]and[a2], and runs them through theconcatfilter.n=2indicates two input segments,v=0indicates there is no video, anda=1indicates one output audio stream, labeled[outa].-map "[outa]": Maps the final concatenated stream to the output file.
Concatenating Video Files with Different Audio Sample Rates
If you are merging video files that contain audio with different sample rates, you must resample the audio while passing the video streams straight to the concat filter.
Use the following command for video files:
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex \
"[0:a]aresample=48000[a1]; \
[1:a]aresample=48000[a2]; \
[0:v][a1][1:v][a2]concat=n=2:v=1:a=1[outv][outa]" \
-map "[outv]" -map "[outa]" output.mp4Command Breakdown for Video:
[0:v][a1][1:v][a2]: Feeds the original video streams ([0:v]and[1:v]) alongside the newly resampled audio streams ([a1]and[a2]) into the concat filter.concat=n=2:v=1:a=1: Tells FFmpeg to expect two inputs, outputting one video stream (v=1) and one audio stream (a=1).-map "[outv]" -map "[outa]": Ensures both the concatenated video and audio are written to the final container.