Concatenate Different Video Formats with FFmpeg
This article provides a step-by-step guide on how to seamlessly join multiple video files of different formats (such as MP4, MKV, and MOV) into a single file using FFmpeg on Linux. Because FFmpeg’s native concatenation tool requires videos to share identical codecs and parameters, merging diverse formats requires transcoding them to a unified standard first. Below, you will find the exact commands needed to normalize your video properties and merge them efficiently without losing quality.
Step 1: Standardize the Video Formats
Before merging, every video must share the same resolution, frame rate, video codec, and audio codec. If you try to join them directly, the resulting file will likely corrupt or lose audio. The most reliable method is to re-encode each video into a standardized format (like H.264 video and AAC audio) at a consistent resolution.
Run the following command for each of your input files, replacing
input1.mkv with your source file and part1.mp4
with your standardized output name:
ffmpeg -i input1.mkv -c:v libx264 -crf 23 -preset medium -vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2" -c:a aac -b:a 192k part1.mp4Repeat this process for your other files (e.g.,
input2.mov), outputting them as part2.mp4,
part3.mp4, and so on.
-vf "scale=...": This video filter forces all videos into a uniform 1920x1080 resolution, adding black bars (letterboxing/pillarboxing) if the original aspect ratios do not match.-c:v libx264 -c:a aac: This converts all video streams to H.264 and audio streams to AAC, ensuring total compatibility.
Step 2: Create a Text Registry File
Once all your video segments are standardized into identical formats, you need to list them in a text file so FFmpeg knows the order in which to join them.
Create a file named inputs.txt in the same directory as
your standardized videos and open it in a text editor. List your files
using the following format:
file 'part1.mp4'
file 'part2.mp4'
file 'part3.mp4'
Step 3: Concatenate the Standardized Videos
Now that the videos have identical properties and are listed in
order, you can use FFmpeg’s concat demuxer. Because the
files are already perfectly matched, FFmpeg can join them instantly
without re-encoding them a second time.
Run the following command in your terminal:
ffmpeg -f concat -safe 0 -i inputs.txt -c copy final_output.mp4-f concat: Activates the concatenation demuxer.-safe 0: Allows the use of relative file paths in your text file.-c copy: Stream-copies the audio and video tracks directly. This prevents any further quality loss and completes the merging process in just a few seconds.