How to Concatenate mp4 Files Using FFmpeg Easily in 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.

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


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


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 by clicking here.

Until next time, take care and stay safe!

krishna rao vijayanagar
Krishna Rao Vijayanagar

Krishna Rao Vijayanagar, Ph.D., is the Editor-in-Chief of OTTVerse, a news portal covering tech and business news in the OTT industry.

With extensive experience in video encoding, streaming, analytics, monetization, end-to-end streaming, and more, Krishna has held multiple leadership roles in R&D, Engineering, and Product at companies such as Harmonic Inc., MediaMelon, and Airtel Digital. Krishna has published numerous articles and research papers and speaks at industry events to share his insights and perspectives on the fundamentals and the future of OTT streaming.

8 thoughts on “How to Concatenate mp4 Files Using FFmpeg Easily in 3 Different Ways!”

  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.

  3. Hi,

    I am using second method to concatenate 4 videos with diff codec, size, FPS. Query is running successfully without any error but output video file is not being generated. Could someone please help me with this? I am using this below function.

    const mergeAllVideo = async () => {
    try {
    const all = [
    ‘-i’, ‘./gifs_0.mp4’,
    ‘-i’, ‘./gifs_1.mp4’,
    ‘-i’, ‘./gifs_2.mp4’,
    ‘-i’, ‘./gifs_3.mp4’,
    ‘-filter_complex’, “[0:v][0:a][1:v][1:a][2:v][2:a][3:v][3:a] concat=n=4:v=1:a=1 [vv] [aa]”,
    ‘-map’, ‘[vv]’,
    ‘-map’, ‘[aa]’,
    const proc = spawn(cmd, all);

    proc.stdout.on(‘end’, function () {
    console.log(“Added mergeAllVideo !!! \n”);
    end = new Date().getTime();
    const diffinsec = (end-start)/1000
    console.log(“Execution time : “,diffinsec,’s’);

    proc.stdout.on(‘error’, function (err) {
    console.log(” ::::: Error at all : “, err);
    } catch (err) {
    console.log(“:::::::: getting error at mergeAllVideo() “, err);

  4. Thanks for the tips. ‘-f concat’ and the use of a file list for ‘-i’ seem to be otherwise undocumented. Your first ffmpeg example worked fine for me.

    There’s a minor error:
    Under “Concatenate all files in a directory”, the ‘for’ statement is missing the final ‘done’.

    A tip for using ‘ls’ to create a list of files: ‘ls -v’ will sort any numerical components in numerical order (without ‘-v’, “10” would precede “9”).

  5. Thanks for this article. But I am facing issue ,please help if you have any solution. I created 2 videos(vid1.mp4 and vid2.mp4) by concating a image and a audio file(like ffmpeg -i img1.png -i audio1.mp3 vid1.mp4). Now I am concating these 2 videos into one(like ffmpeg -f concat -i mylist.txt -c copy output.mp4) where mylist.txt has(
    file vid1.mp4
    file vid2.mp4)
    Here output.mp4 has concated audio well(i.e.audio1.mp3 and audio2.mp3) but only 1st video (i.e. vid1.mp4) frame/image is present and vid2.mp4 image(i.e. img2.png) is not concated. Please help

  6. Pingback: What is FFmpeg? Usage, Benefits, and Installation Simplified - USA Domain Hosting

Leave a Comment

Your email address will not be published. Required fields are marked *

Enjoying this article? Subscribe to OTTVerse and receive exclusive news and information from the OTT Industry.