Concatenate Videos using FFmpeg while Keeping the Original Frame Rate
In this article, we will discuss how to concatenate videos using FFmpeg while keeping the original frame rate. This is a useful technique for those who want to merge multiple video clips into one while preserving the quality of the original videos.
What is FFmpeg?
FFmpeg is a free and open-source software project that produces libraries and programs for handling multimedia data. It includes libavcodec, an audio/video codec library used by several other programs, libavformat, an audio/video container mux and demux library, and the ffmpeg command-line program for transcoding multimedia files.
Concatenating Videos using FFmpeg
To concatenate videos using FFmpeg, we can use the concat filter. This filter takes a list of input videos and concatenates them into a single output video. Here is an example command:
ffmpeg -i input1.mp4 -i input2.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.mp4In this command, we are concatenating two input videos (input1.mp4 and input2.mp4) into a single output video (output.mp4). The concat filter is used to specify the input videos and the number of inputs (n=2). The v and a options specify that we want to concatenate both the video and audio streams.
Keeping the Original Frame Rate
By default, FFmpeg will change the frame rate of the output video to match the frame rate of the first input video. However, we can keep the original frame rate of each input video by using the setpts filter.
Here is an example command:
ffmpeg -i input1.mp4 -i input2.mp4 -filter\_complex "[0:v]trim=start=175.2:end=200.9,setpts=PTS-STARTPTS[v1];[0:a]atrim=start=175.2:end=200.9,asetpts=PTS-STARTPTS[a1];[1:v]trim=start=175.2:end=200.9,setpts=PTS-STARTPTS[v2];[1:a]atrim=start=175.2:end=200.9,asetpts=PTS-STARTPTS[a2];[v1][a1][v2][a2]concat=n=2:v=1:a=1[outv][outa]" -map "[outv]"-map "[outa]" output.mp4In this command, we are using the trim filter to select a specific portion of each input video. We are then using the setpts filter to set the presentation timestamp of each video stream to its original value. Finally, we are using the concat filter to concatenate the video streams and the audio streams into a single output video.
Subtitles
FFmpeg can also handle subtitles. To include subtitles in the output video, we can use the subtitles filter.
ffmpeg -i input1.mp4 -i input2.mp4 -filter\_complex "[0:v]trim=start=175.2:end=200.9,setpts=PTS-STARTPTS[v1];[0:a]atrim=start=175.2:end=200.9,asetpts=PTS-STARTPTS[a1];[1:v]trim=start=175.2:end=200.9,setpts=PTS-STARTPTS[v2];[1:a]atrim=start=175.2:end=200.9,asetpts=PTS-STARTPTS[a2];[v1][a1][v2][a2]concat=n=2:v=1:a=1[outv][outa];[0:s]subtitles=out.ass[s1];[1:s]subtitles=out.ass[s2];[s1][s2]concat=n=2:v=0:a=0[sub]" -map "[outv]"-map "[outa]" -map "[sub]" output.mp4In this command, we are using the subtitles filter to select the subtitles from each input video and concatenate them into a single subtitle stream. We are then using the concat filter to concatenate the video streams, the audio streams, and the subtitle stream into a single output video.
- FFmpeg is a free and open-source software project that produces libraries and programs for handling multimedia data.
- We can use the concat filter to concatenate videos using FFmpeg.
- We can keep the original frame rate of each input video by using the setpts filter.
- FFmpeg can also handle subtitles.