Concatenate videos using FFMPEG

In this post I am going to explain how to concatenate two or more videos. This operation sometimes referred as “join videos” or “merge videos” is an operation that can be a bit complex depending on which videos we want to merge, depending on their formats (codecs), or their resolution.

Simple Join of two videos

We start with this “simple join”. We call it because it is an easy operation. We can join two videos with this command if we have two (or more) input videos of avi or mpg format.

ffmpeg -i “concat:video1.avi|video2.avi” output_video.avi

With other formats (like the popular mp4, webm or mkv) it is not possible to merge videos like this because of the formats. That's why we prefer using another method using the ffmpeg concat filter.

Using the concat filter

We can call this the complex way of merging files as the command we use is a little longer, but with it you can merge video files of any type (it doesn't even matter that they have different formats or codecs) as ffmpeg will encode the result. Use the command:

ffmpeg -i video1.avi -i video2.avi -filter complex “[0:v:0] [0:a:0] [1:v:0] [1:a:0] concat=n=2:v=1:a=1 [v] [a]” -map “[v]” -map “[a]” output_video.avi

We are using the parameters:

  • -filter_complex with the filter specification in double quotes (they are needed):

    • [0:v:0] [0:a:0] [1:v:0] [1:a:0] this part tells the filter to use the video stream from the first file “[0:v:0]”, the audio stream from the first file “[0:a:0]”, the video stream of the second file “[1:v:0]” and the audio stream from the second file “[1:a:0]”.
      Note that if any of the files does not have audio or video stream we should not include here.
      And also note that if we are merging more than two files we should include the streams from the third, four or n files.
      Also note that the first file is 0 (of [0:v:0] indicating 0- first file, v- video stream, and the second zero indicationg the first video stream of that file)

    • concat=n=2:v=1:a=1 [v] [a] this part is telling first to use the ffmpeg concat filter. With the n=2 we are telling ffmpeg concat filter that we have 2 input files (so we have to change it if we have more) with one video stream (v=1) and one audio stream (a=1). Finally we include the [v] and [a] so ffmpeg can use the resulting video ([v]) and audio streams ([a]) for future operations.

  • -map “[v]” -map “[a]” parameters are telling ffmpeg to use the resulting [v] and [a] streams from the concat filter rather than use the input files for output.

  • output_video.avi is the name of the resulting video. Note that we can include any format or encoding parameter before this. With the concat filter ffmpeg will encode the result so we can transform the files to whatever format and codec we want.

With this ffmpeg concat filter we can merge two or more files without worrying on their formats or codecs, so it is our favourite method to join videos.

So what command should I use if I want to merge 3 files

ffmpeg -i video1.avi -i video2.mp4 -i video3.webm -filter complex “[0:v:0] [0:a:0] [1:v:0] [1:a:0] [2:v:0] [2:a:0] concat=n=3:v=1:a=1 [v] [a]” -map “[v]” -map “[a]” output_video.mp4

Note that we have included “[2:v:0] [2:a:0]” and we have changed n=2 for n=3. In this case we have included 3 files each of one different format.

And if I want to merge files without audio

ffmpeg -i video1.avi -i video2.gif -filter_complex “[0:v:0] [1:v:0] concat=n=2:v=1 [v]” -map “[v]” output_video.mp4

Note in this case that we are not using the audio streams (the parts [0:a:0] and [1:a:0] are missing), and that we have not included a=1 after the concat part, and we haven't included the -map “[a]” either.

This is the command to use when we want to merge two files without audio streams, like joining two animated gif files.

Merging videos with different resolution

Now, what happens if we have input video files with different resolutions, and/or different aspect ratios? Well, the ffmpeg concat filter will show an error like this in this situation:

[Parsed_concat_0 @ 0x3c000a0] Input link in1:v0 parameters (size 1280x720, SAR 1:1) do not match the corresponding output link in0:v0 parameters (460x460, SAR 1:1)
[Parsed_concat_0 @ 0x3c000a0] Failed to configure output pad on Parsed_concat_0
Error configuring complex filters.
Invalid argument

So before trying to join various video files we must make sure that they all have the same size. If they do not have the same sizing we can always resize them before joining. Read how to resize a video file with ffmpeg.

当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
若有不妥, 欢迎评注提醒:


订阅 substack 体验古早写作:

点击注册~> 获得 100$ 体验券: DigitalOcean Referral Badge

关注公众号, 持续获得相关各种嗯哼:


关于 ~ DebugUself with DAMA ;-)
公安备案号: 44049002000656 ...::