Concatenating videos in FFmpeg while preserving the original frame rate requires ensuring all input clips share identical video parameters. By using the concat demuxer with a properly formatted list file, you can merge footage without re-encoding, which maintains the source quality and timing. This approach avoids the common pitfall of frame rate conversion that occurs when using simple pipe concatenation.
Why Frame Rate Changes Occur
When users attempt to join video files using basic methods like piping output from one instance to another, FFmpeg often attempts to normalize the stream. If the input files have different resolutions, codecs, or frame rates, the software must re-encode the data to create a unified stream. This process frequently results in an incorrect or averaged frame rate, leading to stuttering playback or audio desynchronization. To keep the original frame rate intact, you must avoid this transcoding step entirely by treating the inputs as compatible containers. The most reliable method for Linux environments is using the concat demuxer. Unlike the protocol-based concatenation which streams data through memory, the demuxer reads metadata from a text file and instructs FFmpeg to simply copy the packets from each file into the new container. Because no decoding or encoding happens, the video frames remain exactly as they were in the source files, including their precise timestamps and frame rates.Preparing Your Input Files
Before running any commands, verify that all video segments you intend to join are encoded with the same codec, resolution, and pixel format. For example, if one clip is H.264 at 1920x1080 and another is HEVC at 1280x720, direct concatenation will fail or produce corrupted output. You can check these details using ffprobe:ffprobe -v error -select_streams v:0 -show_entries stream=codec_name,width,height,r_frame_rate -of default=noprint_wrappers=1 input.mp4
If the outputs match across all your files, you are ready to proceed. If they differ, you must first standardize them using a filter complex or by re-encoding to a common baseline, though this will alter the original frame rate and quality. Ensure your filenames do not contain spaces or special characters, as this can cause parsing errors in the list file.
Creating the Concat List File
Create a plain text file namedinput.txt (or similar) on your Linux system. Each line in this file must specify the path to a video file using the file directive followed by the absolute or relative path enclosed in single quotes. The order of lines determines the playback order of the final video.
file '/path/to/clip1.mp4'
file '/path/to/clip2.mp4'
file '/path/to/clip3.mp4'
Do not include blank lines between entries, as some versions of FFmpeg may interpret them incorrectly. Ensure there is a newline character at the very end of the file. This small detail is often overlooked but is critical for the parser to recognize the last entry. Save the file in the same directory as your videos or adjust the paths accordingly.
Executing the Concatenation Command
With the list file prepared, run the following FFmpeg command in your terminal. This command tells FFmpeg to use the concat demuxer, read the instructions from your text file, and copy the video and audio streams directly into the output file without modification.ffmpeg -f concat -safe 0 -i input.txt -c copy output.mp4
The -f concat flag specifies the format. The -safe 0 option allows the use of absolute paths in the list file; omitting this may cause security warnings or errors if you are using full directory paths. The -c copy instruction is crucial because it forces stream copying rather than re-encoding. This ensures the original frame rate, bitrate, and quality are preserved exactly as they existed in the source clips.
If you encounter issues with certain formats like MP4, ensure the files are MPEG-4 compliant. Some older or non-standard MP4 files may not support direct concatenation due to missing moov atoms or incompatible header structures. In such cases, you may need to remux them first using ffmpeg -i input.mp4 -c copy fixed.mp4 before adding them to the list.
For more advanced editing needs, such as removing specific frames to smooth out transitions, you might explore techniques detailed in our guide on How to Delete Every 25th Frame in FFMPEG for Smoother Video Playback. This can help resolve minor sync issues after concatenation if the sources had slight timing drifts.
Troubleshooting Common Errors
Users often face the "Invalid data found" error during concatenation. This usually indicates that the input files are not truly compatible, even if they appear similar. Check for differences in audio sample rates or channel layouts. A mismatch here will prevent the muxer from creating a valid output file. Another common issue is the "Unsafe file name" error. This happens when FFmpeg detects potential directory traversal attacks in your list file. Using absolute paths without the-safe 0 flag triggers this. Always include this flag when working with complex directory structures on Linux.
If the output file plays back at double speed or has audio/video desync, check the frame rates again. Sometimes, variable frame rate (VFR) recordings from cameras can confuse the demuxer. Converting VFR to constant frame rate (CFR) using a filter before concatenation can stabilize the timeline. For insights on handling timestamp issues, refer to Incorrect pts filename with ffmpeg's -ss and -frame_pts true, which discusses how PTS (Presentation Timestamp) errors affect playback accuracy.
Frequently Asked Questions
Can I concatenate videos with different resolutions?
No, FFmpeg cannot directly concatenate clips with different resolutions using the demuxer method. You must first resize and pad the videos to a common dimension using thescale and pad filters before joining them.
What if my video has variable frame rate?
Variable frame rate (VFR) can cause synchronization issues during concatenation. Convert your clips to constant frame rate (CFR) using the-r flag or a filter graph before creating the list file to ensure stable playback.
Does this method work for MKV files?
Yes, the concat demuxer works well with MKV, AVI, and other container formats, provided the underlying video and audio codecs are identical across all input files.How do I add audio tracks during concatenation?
The standard command copies existing audio streams automatically. If you want to replace audio, you can map specific streams using the-map option, but mixing different audio codecs still requires re-encoding.