The "Can't concatenate videos" error in FFmpeg usually occurs because the input files have mismatched codecs, resolutions, or container formats. To resolve this, you must first ensure all source videos are identical in technical specifications, then use either the concat demuxer for simple merging or the filter_complex method for more complex operations.
Understanding the Root Cause
FFmpeg is incredibly powerful, but it demands strict consistency when joining media files. The most common reason for concatenation failure is that the video streams do not share the same codec, resolution, frame rate, or pixel format. For instance, trying to merge an H.264 file with a VP9 file directly often results in errors because the underlying binary structures differ significantly. Even if both files are MP4 containers, differences in audio sample rates or video dimensions will prevent FFmpeg from stitching them together seamlessly. Another frequent culprit is the use of variable frame rates (VFR). Many screen recordings and mobile phone videos use VFR to save space. FFmpeg’s default concat demuxer expects constant frame rates (CFR) for reliable synchronization. If your inputs are VFR, the timestamps may drift, causing playback issues or immediate failures during the muxing process. Additionally, using incompatible protocols or corrupted headers in the input files can trigger generic concatenation errors. It is essential to verify that each individual video plays correctly on its own before attempting any batch processing.Verifying File Consistency
Before running any merge commands, inspect your files usingffprobe. This tool provides detailed metadata about each stream. Run the following command to check a single file:
ffprobe -v error -select_streams v:0 -show_entries stream=codec_name,width,height,r_frame_rate,sample_rate -of default=noprint_wrappers=1:nokey=1 input.mp4
Compare the output for all files you intend to merge. They must match exactly in codec name, width, height, and frame rate. If the audio streams differ, check the sample rate and channel layout as well. Any discrepancy here indicates that you cannot use the simplest concatenation methods and must proceed to re-encoding or advanced filtering.
Method 1: Using the Concat Demuxer
The concat demuxer is the fastest way to join files without re-encoding, provided they are compatible. This method requires creating a text file that lists your inputs. Create a file namedlist.txt with the following content:
file 'input1.mp4'
file 'input2.mp4'
file 'input3.mp4'
Ensure each line starts with file followed by a space and the path to the video. Use absolute paths if you encounter permission errors. Then, run the merge command:
ffmpeg -f concat -safe 0 -i list.txt -c copy output.mp4
The -c copy flag tells FFmpeg to stream-copy the audio and video data, avoiding quality loss and speeding up the process. The -safe 0 option allows the use of relative paths in older versions of FFmpeg, though modern versions handle this automatically. If this fails, check the error message; it will likely point to a specific stream mismatch that needs addressing via Method 2.
Method 2: Re-encoding with Filter Complex
When files are incompatible, you must re-encode them into a uniform format. This is slower but guarantees compatibility. Use thefilter_complex option to chain filters that normalize the streams. Suppose you have two videos with different resolutions. You need to scale them to a common size and pad them if necessary.
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v]scale=1920:1080,format=yuv420p[v0];[1:v]scale=1920:1080,format=yuv420p[v1];[v0][0:a][v1][1:a]concat=n=2:v=1:a=1[outv][outa]" -map "[outv]" -map "[outa]" output.mp4
In this example, both video streams are scaled to 1920x1080 and converted to the YUV 4:2:0 pixel format, which is widely supported. The concat filter then joins these normalized streams. Adjust the scale parameters to match your desired output resolution. Always specify the pixel format explicitly to avoid artifacts, especially when dealing with high-bit-depth sources.
Handling Audio Synchronization
Audio mismatches are often overlooked. If your videos have different sample rates, FFmpeg may fail to mux them. Add an audio filter to resample the streams to 44100 Hz or 48000 Hz, which are standard values. Includearesample=48000 in your filter chain to ensure smooth audio playback across the concatenated result.
For more troubleshooting on general FFmpeg stability, you might find our guide on resolving startup crashes helpful, although that addresses different system-level errors. See Fixing FFmpeg: Application unable to start (0xc0000005) Issue for background context on environment variables.
Common Pitfalls and Solutions
One common issue is the "unsafe" warning when using relative paths. While newer versions of FFmpeg allow this, sticking to absolute paths in yourlist.txt file eliminates ambiguity. Another pitfall is forgetting to close the quotes around filenames containing spaces. Always wrap paths in single quotes within the list file.
If you encounter infinite loops during processing, it may indicate a corrupted header rather than a concatenation issue. Reviewing loop-related problems can help distinguish between bad inputs and bad commands. Check Troubleshooting FFmpeg Infinite Loop Video Issue if your terminal hangs unexpectedly.
Finally, always test your output file after creation. Play it back to ensure there are no black frames, audio desyncs, or abrupt cuts at the join points. If the result looks correct, you have successfully resolved the concatenation error.
Frequently Asked Questions
Why does FFmpeg say "Invalid data found when processing input"?
This error typically means one of your source files is corrupted or has an unsupported header structure. Verify the file integrity by playing it in a standard media player like VLC. If it fails there too, the file itself is damaged and needs to be replaced or repaired before concatenation.Can I concatenate videos with different durations?
Yes, FFmpeg can easily join videos of varying lengths. The concat demuxer simply appends the second file immediately after the first ends. However, ensure that the technical specs (codec, resolution) remain consistent, regardless of the time length difference.What is the best format for intermediate files?
MP4 with H.264 video and AAC audio is the most universally compatible container format. If you need to re-encode, stick to these codecs to ensure maximum playback support across devices and web platforms. Avoid proprietary or obscure codecs unless specifically required by your workflow.How do I fix audio desync after concatenation?
Audio desync often results from differing sample rates or timestamp inconsistencies. Ensure all inputs share the same sample rate, such as 48kHz. If desync persists, try adding the-async 1 flag to the output command to force FFmpeg to adjust audio timestamps dynamically.