When you need to merge two videos without re-encoding, you can use FFmpeg's concat protocol. This method is suitable for video files with identical formats, such as frame rate, resolution, and encoding format must all be identical.
Steps:
-
Prepare the video files
First, ensure that both video files have identical formats. You can use theffprobecommand to inspect video information. -
Create a file list
List the paths of all videos to be merged in a text file, with each path on a separate line and prefixed with thefilekeyword. For example, create a file namedfilelist.txtwith the following content:shellfile 'video1.mp4' file 'video2.mp4' -
Use FFmpeg to merge
Execute the following command to merge the videos:bashffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp4Here,
-f concatspecifies the use of the concat protocol,-safe 0allows the use of absolute paths or non-standard characters,-i filelist.txtspecifies the input file list, and-c copyinstructs FFmpeg to copy the original stream data without re-encoding.
Example:
Suppose you have two video clips, clip1.mp4 and clip2.mp4, both with the same H.264 encoding and 720p resolution. You can follow these steps:
-
Check the format
ffprobe clip1.mp4andffprobe clip2.mp4 -
Create the file list
Editfilelist.txtand add:shellfile 'clip1.mp4' file 'clip2.mp4' -
Execute the FFmpeg command
Run the following command in the terminal:bashffmpeg -f concat -safe 0 -i filelist.txt -c copy merged_video.mp4
This will produce merged_video.mp4 as the merged video without re-encoding, preserving the original video quality. The advantage of this method is that it is fast and does not degrade video quality. However, the drawback is that all video files must be identical in terms of encoding, container format, frame rate, etc. If there are mismatches, you may need to convert them to the same format first.