FFmpeg Concat Demuxer with Different Audio Tracks
This article explains how to handle files with differing audio
configurations when using FFmpeg’s concat utility. Because
the concat demuxer requires all input files to have
identical stream parameters, merging files with different audio codecs,
sample rates, or channel layouts requires specific preparation. You will
learn how to standardize your files for the demuxer or use the
alternative concat filter to merge mismatched files
dynamically.
The Core Limitation of the Concat Demuxer
The FFmpeg concat demuxer (-f concat)
performs a stream copy without re-encoding. For this to work safely,
all input files must have the exact same codecs, sample rates,
channel layouts, and stream counts.
If you try to concatenate files with mismatched audio (for example, one file with stereo AAC at 44.1 kHz and another with 5.1 channel AC-3 at 48 kHz), the output file will suffer from audio desynchronization, distortion, or silence.
To resolve this, you have two options: standardize the inputs before using the demuxer, or use the concat filter to re-encode the files on the fly.
Method 1: Standardizing Input Files (Recommended for Demuxer)
To use the fast, lossless concat demuxer, you must first
transcode the audio of your input files so they match perfectly.
Step 1: Normalize the Audio of Each File
Convert the audio of all input files to a uniform format (e.g., AAC codec, stereo channel layout, 48 kHz sample rate) while copying the video stream to save time.
Run this command on each of your source files:
ffmpeg -i input1.mp4 -c:v copy -c:a aac -ac 2 -ar 48000 output1.mp4
ffmpeg -i input2.mp4 -c:v copy -c:a aac -ac 2 -ar 48000 output2.mp4-c:v copy: Copies the video stream without re-encoding.-c:a aac: Converts the audio to AAC.-ac 2: Forces the audio to stereo (2 channels).-ar 48000: Sets the audio sample rate to 48,000 Hz.
Step 2: Create the Concat Text File
Create a text file named inputs.txt containing the paths
to your standardized files:
file 'output1.mp4'
file 'output2.mp4'
Step 3: Run the Concat Demuxer
Run the demuxer to merge the files instantly without further re-encoding:
ffmpeg -f concat -safe 0 -i inputs.txt -c copy final_output.mp4Method 2: Using the Concat Filter (No Pre-Processing Required)
If you do not want to create intermediate standardized files, you can
use FFmpeg’s concat filter. This method decodes the inputs,
processes them together, and encodes the final output. It handles
differing audio tracks automatically but requires more CPU power and
time.
Run the following command to merge two files with different audio tracks:
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v][0:a][1:v][1:a] concat=n=2:v=1:a=1 [v][a]" -map "[v]" -map "[a]" -c:v libx264 -c:a aac output.mp4How the filter parameters work:
-i input1.mp4 -i input2.mp4: Specifies the input files.[0:v][0:a][1:v][1:a]: Tells FFmpeg to take the video and audio from the first input (0), and the video and audio from the second input (1).concat=n=2:v=1:a=1: Specifies that we are concatenatingn=2segments, resulting inv=1video stream anda=1audio stream.[v][a]: Names the temporary output video and audio streams.-map "[v]" -map "[a]": Maps these output streams to the final file.
Handling Files with Missing Audio Tracks
If one of your input files has no audio track at all, the demuxer will fail. You must add a silent audio track to the video before attempting to concatenate.
To generate a silent audio track that matches a working stereo 48 kHz file, use this command:
ffmpeg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=48000 -i silent_input.mp4 -c:v copy -c:a aac -shortest input_with_silent_audio.mp4Once the silent audio track is generated, you can proceed with the standardizing or filtering methods detailed above.