Concatenate MP4 Files with FFmpeg in Linux

This article provides a quick overview and step-by-step guide on how to efficiently combine multiple MP4 video files into a single file using FFmpeg in a Linux environment. It covers the two most reliable methods—the Demuxer method for files with identical codecs and the Protocol method for files that require transcoding—ensuring you can merge your videos seamlessly without losing quality or sync.

The Demuxer Method (Best for Identical Files)

If your MP4 files have the exact same resolution, frame rate, and video/audio codecs (for example, videos recorded on the same camera or smartphone), the concat demuxer is the absolute best approach. It avoids re-encoding the video, meaning the process finishes in seconds and preserves 100% of the original quality.

First, you need to create a text file listing all the video files you want to merge in the order you want them to appear. Open your terminal and create a file named inputs.txt:

file 'video1.mp4'
file 'video2.mp4'
file 'video3.mp4'

Once the text file is ready, run the following FFmpeg command in the same directory:

ffmpeg -f concat -safe 0 -i inputs.txt -c copy output.mp4

The Protocol Method (Best for Mismatched Files)

If your MP4 files come from different sources and have varying resolutions or codecs, the demuxer method might cause playback issues. In this case, you must use the concat filter, which transcodes the files so they match perfectly in the final output.

You can merge mismatched files directly with a single command without creating a text file:

ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1[outv][outa]" -map "[outv]" -map "[outa]" output.mp4