Video Editing

8 min read

How to combine videos using FFmpeg concat?

Learn how to effortlessly combine multiple videos into a single file using FFmpeg concat. This step-by-step guide provides clear instructions and code examples for efficient video merging.

How to combine videos using FFmpeg concat?

FFmpeg Installation

To install FFmpeg on an operating system such as Ubuntu, you’ll need administrative privileges (root or sudo). Follow these simple steps to get FFmpeg installed:

Begin by refreshing your system's package list to ensure you have the latest repository information:

sudo apt update

Next, you can install FFmpeg by using the following command:

sudo apt install ffmpeg

Once the installation process finishes, you can confirm the version of FFmpeg that was installed by running:

ffmpeg -version

This will display the version of FFmpeg along with additional details, such as:

ffmpeg version 4.4.2-1ubuntu3 Copyright (c) 2000-2022 the FFmpeg developers
built with gcc 9 (Ubuntu 9.3.0-17ubuntu1~20.04)
configuration: --enable-gpl --enable-libx264 --enable-libx265

You can also install FFmpeg using Snap:

sudo snap install ffmpeg

How to combine similar codec videos with FFmpeg concat?

FFmpeg offers a couple of different ways to merge videos that use the same codec: the concat demuxer and the concat protocol. These methods are ideal for combining multiple video files without re-encoding which helps keep the original quality intact. 

Key Considerations Before You Start:

Ensure that the video files you want to merge share the following properties:

  • Frame rate (timebase)
  • Resolution (height and width)
  • Codec type
  • Pixel format (color space)

This ensures compatibility when merging the files.

Method-1: Using Concat Demuxer

The concat demuxer is the most straightforward way to combine videos that have identical properties. It allows you to merge without re-encoding, which means there's no loss in video quality and the process is very fast.

Step 1: First, you need to create a text file that lists the video files you want to merge. This file must follow a specific format, where each line contains the word file followed by the video file path in single quotes.

You can automatically generate this list with the following command:

printf "file '%s'\n" *.mp4 > merge_list.txt

This command adds all .mp4 files from the current directory into merge_list.txt. Here's an example of what this file might look like:

file 'Video1.mp4'
file 'Documentary2.mp4'
file 'Clip3.mp4'

Step 2: Next, you can use the ffmpeg command to merge the videos based on the list created in the previous step:

Concatenating Using Concat Demuxer

Now, let’s use the -f concat flag to merge the videos from our list.txt file:

ffmpeg -f concat -safe 0 -i merge_list.txt -c copy final_output.mp4

Here,

  • -f concat: This flag tells FFmpeg to use the concat demuxer.
  • -safe 0: This option disables the safety check for paths, allowing the use of files with special characters.
  • -c copy: This ensures no re-encoding takes place, resulting in a faster and lossless process.

The files are stitched together in the order they appear in merge_list.txt. Also, since you’re using -c copy, the merging process is lossless.

Method-2: Using Concat Protocol

The concat protocol is another method for combining video files, but it functions at a lower level than the demuxer method. The concat protocol reads video files as a continuous stream, so this means it is not suitable for container formats like MP4 but works well with simpler formats such as MPEG or TS. Here's a simple guide to get started:

Step 1: To merge videos with the concat protocol, use the following FFmpeg command. This example assumes you are working with files in the MPEG format:

ffmpeg -i "concat:Travel1.mpeg|Travel2.mpeg|Trip3.mpeg" -c copy output_video.mkv

Here,

  • concat:: This tells FFmpeg to treat the input files as part of a continuous stream.
  • Files: List the file names, separated by the pipe (|) symbol.
  • -c copy: Again, this option is used to avoid re-encoding.

On some systems, you might need to escape the pipe (|) symbol with a backslash (\), depending on your shell:

ffmpeg -i "concat:Travel1.mpeg\|Travel2.mpeg\|Trip3.mpeg" -c copy output_video.mkv

Supported Formats and Limitations

  • Concat Demuxer: It is suitable for formats like MP4, MKV, or MOV, but only if the video files share identical codecs and properties.
  • Concat Protocol: It is limited to formats that allow raw concatenation, such as MPEG or TS. Not compatible with complex formats like MP4.

How to combine different codec videos with FFmpeg concat?

When working with video files that have varying codecs, resolutions, or audio configurations, the concat filter in FFmpeg is your best option. Unlike simpler concat methods, the concat filter re-encodes the videos. This makes way for compatibility despite differences between the input files. Of course, this re-encoding step means the process is slower, but it is essential for handling files with different properties.

Using the concat video filter

The concat filter should be used when:

  • The input files use different codecs (e.g., combining H.264 with VP9 videos).
  • The videos have different frame rates, resolutions, or aspect ratios.
  • You need to apply filters to the input files before merging (for instance, resizing or adjusting frame rates)

Note: The input videos must all start at timestamp zero (0:00) for smooth concatenation.

Now, the concat filter processes both video and audio streams. When combining files, you'll need to specify how many video and audio streams to merge.

ffmpeg -i input1.ext -i input2.ext -i input3.ext -filter_complex \
  "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] concat=n=3:v=1:a=1 [v] [a]" \
  -map "[v]" -map "[a]" output.ext

Here,

  • n=3: The number of input files.
  • v=1: The number of video streams to output.
  • a=1: The number of audio streams to output.
  • -map: Used to map the filtered streams to the output.

Let ffmpeg concatenate videos with a single audio stream. As an example, we will combine three MKV video files with one audio stream each: intro.mkv, scene.mkv, and credits.mkv.

ffmpeg -i intro.mkv -i scene.mkv -i credits.mkv -filter_complex \
  "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] concat=n=3:v=1:a=1 [v] [a]" \
  -map "[v]" -map "[a]" final_video.mkv

Here,

  • -i: Specifies the input files.
  • -filter_complex: A more advanced filter for complex processing.
  • concat=n=3:v=1:a=1: Combines the three input files, then outputs one video and one audio stream.
  • -map: This maps the resultant video and audio streams to the output file.

The final output, final_video.mkv, will contain the concatenated video with one audio stream.

Next, let ffmpeg combine videos with multiple audio streams. Let’s say you have three video files (segment1.mkv, segment2.mkv, and segment3.mkv), each with two audio streams (e.g., different language tracks). Here’s how you would merge them:

ffmpeg -i segment1.mkv -i segment2.mkv -i segment3.mkv -filter_complex \
  "[0:v] [0:a] [0:a] [1:v] [1:a] [1:a] [2:v] [2:a] [2:a] concat=n=3:v=1:a=2 [v] [a1] [a2]" \
  -map "[v]" -map "[a1]" -map "[a2]" merged_output.mkv

Here,

  • n=3: Concatenating three input files.
  • v=1: One video stream for the output.
  • a=2: Two audio streams for the output.

Now, the ffmpeg concat video filter is indeed powerful, but there are some trade-offs to consider:

  • Since the concat filter performs re-encoding, it is slower than methods like the concat demuxer that don't re-encode.
  • It requires more CPU and memory and may take longer—especially in case of high-resolution files.
  • FFmpeg does attempt to adjust the input to match the output format, but it still exhibits a mismatch in frame rate, resolution, or aspect ratio—and this could require additional filtering 

How to combine MP4 files using FFmpeg concat?

As long as the files share similar properties (like resolution, codecs, and frame rates), combining two Mp4 files is a simple task.

Step 1: To begin, you need to create a plain text file that lists the MP4 files you want to concatenate. Each line of this file should start with the word file—followed by the file path enclosed in single quotes. 

For example, let’s assume you have two videos: part1.mp4 and part2.mp4, both located in the /media/videos/ directory. You’ll create a text file, for example videoList.txt, with content like this:

file '/media/videos/part1.mp4'
file '/media/videos/part2.mp4'

You can also manually create this file using a text editor. Or you can use a command in your terminal to automatically list all MP4 files in the directory:

printf "file '%s'\n" /media/videos/*.mp4 > videoList.txt

This will populate videoList.txt with the names of all .mp4 files located in /media/videos/.

Step 2: Once your file list is ready, you can execute the FFmpeg command to merge the videos. Here is the command structure:

ffmpeg -f concat -safe 0 -i videoList.txt -c copy final_output.mp4

Here,

  • -f concat: This tells FFmpeg to use the concat demuxer.
  • -safe 0: This flag disables the check for unsafe file paths (like spaces or special characters).
  • -i videoList.txt: This specifies the text file containing the list of video files.
  • -c copy: This instructs FFmpeg to concatenate without re-encoding
  • final_output.mp4: The name of the combined output file.

For instance, if we use the videoList.txt created earlier, the command will look like this

ffmpeg -f concat -safe 0 -i videoList.txt -c copy full_movie.mp4

Remember, 

  • For this method to work smoothly, both MP4 files should have identical resolution, codecs, etc.
  • Since we’ve used the -c copy option, FFmpeg won’t re-encode the files.

Conclusion

Using FFmpeg to concatenate videos is one of the most flexible and efficient solutions to merge videos. It allows you to join videos (single or multiple files), merge MP4 files, or even merge audio and video streams with minimal effort. 

FAQs

  1. How to concatenate videos with multiple audio stream?

To concatenate videos with multiple audio streams using FFmpeg, you can apply the concat filter like this:

ffmpeg -i intro.mkv -i scene.mkv -i outro.mkv -filter_complex \
"[0:v] [0:a] [0:a] [1:v] [1:a] [1:a] [2:v] [2:a] [2:a] concat=n=3:v=1:a=2 [v] [a1] [a2]" \
-map "[v]" -map "[a1]" -map "[a2]" final_output.mkv

This merges three video files (intro.mkv, scene.mkv, and outro.mkv) with one video and two audio streams per file. The n=3:v=1:a=2 option specifies that there are three files, one video stream, and two audio streams to concatenate. The -map options define the final output streams.

  1. How do I concatenate different frame rates in FFmpeg?

To concatenate videos with different frame rates in FFmpeg, follow these steps:

  • First, convert each video to the same frame rate using the -r option. For example, to set 30 fps:
 ffmpeg -i input1.mp4 -r 30 output1_30fps.mp4
  • Next, make a text file (mylist.txt) with each line listing a video like:
file 'output1_30fps.mp4'
file 'output2_30fps.mp4'
  • Finally, use the concat demuxer to merge the videos:
ffmpeg -f concat -safe 0 -i mylist.txt -c copy output_final.mp4
  1. How to Add transitions between videos using concat filter?

To create a transition effect, such as a crossfade between two videos, follow these steps:

  • Generate a black background with the same resolution and duration as your final output.
  • Ensure each video has an alpha channel for smooth transitions.
  • Add a fade-in or fade-out effect to the alpha channel of each video.
  • Use the overlay filter to combine the videos with the black background.

Here's an example command:

ffmpeg -i video1.mp4 -i video2.mp4 -f lavfi -i color=black -filter_complex \
"[0:v]format=pix_fmts=yuva420p,fade=t=out:st=4:d=1:alpha=1,setpts=PTS-STARTPTS[fadeout];\
[1:v]format=pix_fmts=yuva420p,fade=t=in:st=0:d=1:alpha=1,setpts=PTS-STARTPTS+5/TB[fadein];\
[2:v]scale=1280x720,trim=duration=6[bg];\
[bg][fadeout]overlay[bg1];\
[bg1][fadein]overlay=format=yuv420[out]" \
-vcodec libx264 -map [out] final_output.mp4

How to merge videos online for free?

You can use tools like Canva, Adoba, Clideo or Kapwing. Upload your videos, arrange them, and export the merged file.

Images or Videos Loading Slow?

We compress them so your users don't need to wait.