Concatenating Multiple Video Cuts with Hardcoded Subtitles using FFmpeg
In this article, we will discuss how to concatenate multiple video cuts, including audio tracks, into a single MP4 file using the powerful and versatile FFmpeg tool. We will also cover how to hardcode embedded subtitles into the final video.
Introduction to FFmpeg
FFmpeg is a free and open-source software project consisting of a vast suite of libraries and programs for handling video, audio, and other multimedia files and streams. It is available for most operating systems, including Windows, macOS, and Linux. FFmpeg is a command-line tool that supports a wide variety of codecs, formats, and options, making it a popular choice for video processing and manipulation tasks.
Concatenating Video Cuts
To concatenate multiple video cuts into a single file, we first need to prepare the individual clips by adding a "concat demuxer" header to each clip. This header tells FFmpeg how to concatenate the clips. We can do this using the following command:
ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -filter\_complex "[0:v][0:a][1:v][1:a][2:v][2:a]concat=n=3:v=1:a=1" -c:v libx264 -crf 18 -c:a aac -strict experimental output.mp4In this command, -i specifies the input files (input1.mp4, input2.mp4, and input3.mp4), and the filter\_complex option specifies the concat filter with n=3 (the number of inputs), v=1 (output one video stream), and a=1 (output one audio stream). The -c:v and -c:a options specify the video and audio codecs, respectively. The -crf option controls the video quality, and -strict experimental is required for some versions of FFmpeg to enable AAC encoding.
Hardcoding Embedded Subtitles
To hardcode embedded subtitles into the final video, we need to extract the subtitles from the input files, if available, and then merge them into the final video using the following command:
ffmpeg -i output.mp4 -vf "subtitles=input1.srt:force_style='Fontsize=24,PrimaryColour=&H00FFFF'" final\_output.mp4In this command, -vf specifies the video filter, which includes the subtitles filter that uses the input1.srt subtitle file. The force\_style option sets the font size and color of the subtitles. We can repeat this command for each input subtitle file, stacking the filters like this:
ffmpeg -i output.mp4 -vf "subtitles=input1.srt:force\_style='Fontsize=24,PrimaryColour=&H00FFFF',subtitles=input2.srt:force\_style='Fontsize=24,PrimaryColour=&H00FFFF'" final\_output.mp4FFmpeg is a powerful and versatile tool for handling video, audio, and other multimedia files and streams. By using the concat demuxer and subtitles filter, we can concatenate multiple video cuts, audio tracks, and subtitles into a single MP4 file with hardcoded embedded subtitles.