Concatenate Video Files Using FFMPEG

This article will show you how to concatenate two mpg files into one big file using the CAT and FFMPEG commands. Here was the dilemma. I have two video files that were recorded separately. The recording was accidentally stopped for a brief second or two. The cam created two separate video files. I wanted to stitch and merge the two files into one big file.

To accomplish the feat, I will be using both CAT and FFMPEG commands that is common to most Linux systems. If you use the cat command alone like the in the example below, it will not work. You’ll have timestamp issues. The player also will not go past the end of the first video. It will never play the second and third videos if you have more.

Cat is not enough.

# cat abc.mpg def.mpg > ghi.mpg

What you need to do is use CAT and pipe the output using FFMPEG.

The command:

# cat abc.mpg def.mpg | ffmpeg -f mpeg -i - -vcodec copy -acodec copy ghi.mpg

You now have a file named ghi.mpg which is the combined output of abc.mpg and def.mpg. You can concatenate 3 or more files if you wish. FFMPEG is such a powerful video converter program capable of converting videos to any video format like avi, wmv, mov, etc. Earlier in the year, I wrote how to install FFMPEG and also another explaining the options.

  • Twitter
  • Facebook
  • Google Plus
  • Pinterest
  • PDF
  • RSS

12 Comments

  • At 2009.12.12 13:43, Jano said:

    Nice howto, but it doesn’t work for me. I tried to concatenate 10 parts of a movie downloaded from youtube.com, it starts okay, and then after few seconds I get the following message:

    [NULL @ 0x9617670]error, non monotone timestamps 563083 >= -1931820712
    av_interleaved_write_frame(): Error while opening file

    Any ideas?? TIA

    • At 2010.01.24 10:03, Jan said:

      Thanks. this really works for me.

      • At 2010.02.03 09:39, loris said:

        For me it worked but no other parameter is possible. thanks

        • At 2010.02.03 09:47, loris said:

          It should be possibile to do something like

          cat imput%d.mpg | ffmpeg -f mpeg -i – -vcodec copy -acodec copy outfile.mpg

          Another solution that maybe make a more compact file is
          mencoder -oac copy -ovc copy -o output.avi input1.avi input2.avi

          • At 2010.02.26 06:44, Joe said:

            I’m using this for MASSIVE DV files, but the output is limited to exactly 1GB. Any ideas?

            • At 2010.02.26 09:36, ulyssesr said:

              Not sure why. I’ve heard of 2GB size file limit with 32 bit systems, but not 1GB.

              • At 2010.10.13 12:50, reelfernandes said:

                Thanks for the tip on ffmpeg. Do you know if this would be possible with .wav files?

                Something like? cat 1.wav 3.wav | ffmpeg -f wav -i – -acodec copy combined.wav?

                • At 2010.10.13 15:54, Ulysses said:

                  If you are just joining audio instead of video, you might want to try Audacity. Cut and pasting wav files to Audacity is doable.

                  • At 2011.02.25 16:20, havok1977 said:

                    Hi, cool tutorial but I’m having problems when joining files which contain AC3 5.1 audio, the resulting file is totally mute… any suggestions?

                    • At 2011.07.26 04:17, azz said:

                      you will probably need to concatenate the audio using sox, the video in ffmpeg, and then mux the two together

                    • At 2011.02.27 14:03, Rav Joshua Ben Levi said:

                      Dude! You made my day…keep up the good work

                      • [...] sí mismo para obtener un video de 273 segundos (4 minutos y 33 segundos) de duración. Me basé en esta página y escribí este [...]