Casper Kloppenburg·3 November 2022·3 min readFFmpeg is a free and open-source video editing tool capable of trimming, cropping, concatenating, muxing, and transcoding almost any type of media file you throw at it.
It's also a very robust solution for implementing video automation, as we use it extensively in our own video editing API. For this tutorial we'll use FFmpeg 5.1.2, but any recent version will do.
The following command can be used to merge two videos into one:
$ ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1" -vsync vfr output.mp4
concat filter to concatenate the streams from the input files. This filter requires two or more inputs. Here, we're inputting two videos and their audio stream with [0:v][0:a][1:v][1:a] and then telling concat to merge them with n=2. The v=1:a=1 part tells FFmpeg that each segment has a single video and audio stream.-vsync vfr argument to synchronize the video files. The details of this option are explained in the FFmpeg documentation under -vsync.In the same way as the example above, you can concatenate more than two video files simply by adding more inputs to the concat filter. Using an additional input stream, the following example highlights the difference between the previous example:
$ ffmpeg -i video1.mp4 -i video2.mp4 -i video3.mp4 -filter_complex "[0:v][0:a][1:v][1:a][2:v][2:a]concat=n=3:v=1:a=1" -vsync vfr output.mp4
Here are a few issues you may encounter while using FFmpeg's concat filter.
This error message shows up when there's no audio stream in one of the input videos. If neither of your input videos has an audio stream, make FFmpeg only concatenate the video streams:
$ ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][1:v]concat=n=2:v=1:a=0" -vsync vfr output.mp4
Alternatively, you can make both videos have an audio track by adding a silent track to the video that doesn't have one:
$ ffmpeg -i input.mp4 -f lavfi -i anullsrc -map 0:v -map 1:a -c:v copy -shortest output.mp4
-f lavfi -i anullsrc argument creates a silent audio track.-map 0:v -map 1:a arguments, the video track from the input.mp4 (0:v) and the audio track from the silent track (1:a) are mapped to the output.-c:v copy arguments tell FFmpeg not to re-encode the video.-shortest.This message appears when you concatenate two videos with different frame rates. Additionally, it will provide a message stating "Please consider specifying a lower framerate, a different muxer or setting vsync/fps_mode to vfr". This can be fixed by adding the -vsync vfr argument. More info can be found in the FFmpeg documentation under -vsync. Here's an example:
$ ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1" -vsync vfr output.mp4Whether you prefer code or no-code, we have a wide range of guides on video automation.