How to Concatenate mp4 Files Using FFmpeg – 3 Different Ways!

You can use FFmpeg to concatenate mp4 files very easily! There are many ways to do this including variations such as (1) concatenating only the audio (2) concatenating only video (3) concatenating all the files in a directory (4) concatenating files that do not have the same height, width, etc. and more use cases that we will explore today in this FFmpeg tutorial.

Concatenate mp4 files using FFmpeg

Let’s start with the simplest use case which is to concatenate two mp4 files using FFmpeg. Let’s say you have two files file1.mp4 and file2.mp4.

You can concatenate these files using the concat demuxer (documentation) easily if their properties match. That is, they have the same height, width, pixel formats, codecs, etc.

There are two steps to using this command. First, you need to create a txt file with the names and paths of all the individual files that you want to concatenate. Then, you need to supply this list to FFmpeg as a commandline parameter.

Let’s create the list first. Here is an example and let us call the list fileList.txt. In this example, both the files are in the home directory. each line starts with the keyword file and contains the path of the file within single quotes.

$ cat fileList.txt
file '/home/file1.mp4'
file '/home/file1.mp4'

Now, you can concatenate them using the following FFmpeg command

./ffmpeg -f concat -safe 0 -i fileList.txt -c copy mergedVideo.mp4

Here, you are using the concat command to read the list of video files that you created (fileList.txt) and copy the individual files in that order into an output file called mergedVideo.mp4. Remember, you are not re-encoding in this commandline because you are using the copy command.

Concatenate all files in a directory

This is a commonly asked question and it has to do more with shell scripting than actual FFmpeg usage. The first step you need to do here is to find all the mp4/avi/wav files in your directory and add them to a file. Then you can easily concatenate them as shown above.

for f in *.mp4 ; do echo file \'$f\' >> fileList.txt;

I ran this command in my video database folder and here is the output – looks good right?

file 'brooklynsfinest_clip_1080p.mp4'
file 'parkjoy_1080p50_crf1.mp4'
file 'riverbed_1080p25_crf1.mp4'
file 'simpsons_1080p2398_clip.mp4'
file 'simpsons_trailer.mp4'
file 'touchdown_pass_1080p.mp4'
file 'touchdown_pass_1080p_2997fps.mp4'

And, then you can concatenate them using the concat command we learned about earlier as follows.

./ffmpeg -f concat -safe 0 -i fileList.txt -c copy mergedVideo.mp4

Concatenate mp4 Files of Different Codecs, Resolutions

If your files have different video codecs, or pixel formats, or height and width, then you need to re-encode them before you can concatenate the files. There are two ways to go about this –

  1. Re-encode the files before joining them. This is a simple technique where you re-encode all the files to the required format and then use the concat command to join them all together. I find this useful if I have a large set of videos and I want to precisely control their quality before joining them together.
  2. concat video filter – this is the second option where you end up re-encoding your content and you also preserve the option for filtering the videos before concatenating them. Let’s take a look at this option in detail now.
Related:  CrossFade, Dissolve, and other Effects using FFmpeg's xfade Filter

Here is a sample commandline to get started –

ffmpeg -i file1.mp4 -i file2.mp4 -i file3.mp4 \
       -filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] 
                        concat=n=3:v=1:a=1 [vv] [aa]" \
       -map "[vv]" -map "[aa]" mergedVideo.mp4

Let’s break this command line down, shall we?

First, we provide the names of the input files to FFmpeg

-i file1.mp4 -i file2.mp4 -i file3.mp4

Next, we use the filter_complex filtergraph parameter to instruct FFmpeg from where to take the audio and video. [0:v][0:a] means FFmpeg needs to take the video and audio from the 0th video (file1.mp4). Then, you tell FFmpeg to concat three files (n=3). The v=1:a=1 implies that there is one stream for the audio and one stream for the video.

Finally, [vv] [aa] are the names that we have chosen for the output video and audio.

-filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] concat=n=3:v=1:a=1 [vv] [aa]"

The final step is to map these audio and video outputs to the final video container. This is done as follows –

-map "[vv]" -map "[aa]" mergedVideo.mp4

Note: at this stage, you have the merged video and audio in [vv] [aa] and you can either write them out to the final container, or use them in another filter to further process them!

Concatenate mp4 Files Using Intermediate File Formats

Some container formats such as mp4 cannot be directly concatenated using the Linux kernel’s cat command or FFmpeg’s concat command. An option for this is to convert your files to an intermediate file format and then concatenate them easily. A common intermediate file format is ts (Transport Stream).

Here is a one-liner to convert mp4 files to ts

ffmpeg -i file1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts fileIntermediate1.ts

After finishing the conversions, you can then simply cat them together using the Linux terminal or use the concat command in FFmpeg. Both should work.

cat fileIntermediate1.ts fileIntermediate2.ts > output.ts

or

ffmpeg -i "concat:fileIntermediate1.ts|fileIntermediate2.ts" -c copy -bsf:a aac_adtstoasc mergedVideo.mp4

Conclusion

In this tutorial, we saw three different ways to concatenate video files using FFmpeg. I hope this was useful to everyone. If there are any questions, problems that you would like help with, do let us know in the comments below and we’ll help you out.

Check out the rest of our FFmpeg tutorials on OTTVerse.com by clicking here.

Until next time, take care and stay safe!

krishna rao vijayanagar
Krishna Rao Vijayanagar

I’m Dr. Krishna Rao Vijayanagar, and I have worked on Video Compression (AVC, HEVC, MultiView Plus Depth), ABR streaming, and Video Analytics (QoE, Content & Audience, and Ad) for several years.

I hope to use my experience and love for video streaming to bring you information and insights into the OTT universe.

3 Comments

  1. I am facing problem while contact two video, Video A is the original video and Video B created through ffmpeg. when i try to contact these two video B video not getting merged with A. and if i add some other sample video that time it works. problem is with video B. while creating something is going wrong. one more then video B can play after download but when i dry to play it on Mozilla browser it show error on video No video with supported format and MIME type found.

    please help

  2. Awesome tutorial, thank you so very much.

    Small problem, maybe it was a typo, when concatinating with diff codec/resolution, you did ‘concat=n=3:v=1:a=1 [v] [a]” ‘ instead of ‘concat=n=3:v=1:a=1 [vv] [aa]”, ie the outputs ‘a’ and ‘v’ has to be changed to ‘vv’ and ‘aa’ else it will raise an error like below
    ”’Output with label ‘vv’ does not exist in any defined filter graph, or was already used elsewhere.”’

    Overall, awesome job, thank you so very much. Using this with python system process and its working now well.

Leave a Reply

Your email address will not be published.


*