19

I want to combine two mp4 videos to form a single mp4 video using ffmpeg.

what i tried so far is

ffmpeg -i input1.mp4 -i input2.mp4 output.mp4

But, every time i get the video with video codec of first input and not the other. How can i combine them? Any idea on this will be highly appreciated.

Astha
  • 1,728
  • 5
  • 17
  • 36
  • 2
    What do you mean by "video codec of first input and not the other"? Are they different video formats? Do you get the contents of both videos in the output? – TheJuice Sep 27 '11 at 10:37
  • 1
    Please provide more detail. How are you expecting to combine them? Sequentially? Or do you want to have two parallel video streams in one file? MP4 is just a container. Are the actual codecs the same? Give the output of `ffprobe` for both your files. – mpenkov Sep 27 '11 at 10:37
  • 1
    both videos are of the same format and i get only the first video in the ouptput file i get. – Astha Sep 27 '11 at 10:38
  • i want to combine both the videos sequentially. – Astha Sep 27 '11 at 10:41

5 Answers5

21

As previous answers show, you need to convert first to an intermediate format. If the mp4 contains h264 bitstream, you can use:

ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts input1.ts
ffmpeg -i input2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts input2.ts
ffmpeg -i "concat:input1.ts|input2.ts" -c copy output.mp4

A more detailed answer you can find here.

Community
  • 1
  • 1
user1592546
  • 1,480
  • 1
  • 14
  • 30
  • This is the answer!!! Mostly users have to concatenate .mp4 files but no where could i find solution until I came across this! Thanks a lot – Shreyesh Desai Sep 27 '21 at 05:54
9

Please read the FFMPEG FAQ for information about joining files.

Unfortunately, since you're using MP4 files, simple concatenation won't work for you because the MP4 format contains a "header" (although it doesn't necessarily have to be at the beginning of the file) section that describes and contains offsets into the media data. You will need to transcode both files to a format that can be concatenated and then generate an MP4 file from that format (which will generate an appropriate header section).

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
mpenkov
  • 21,621
  • 10
  • 84
  • 126
6

You can do this with ffmpeg, but there's also a little tool out there, called MP4Box (part of GPAC), that can concatenate multiple MP4 files.

In your case, the syntax is

MP4Box -cat input1.mp4 -cat input2.mp4 output.mp4
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
  • 3
    You can do this with `ffmpeg` using the [concat demuxer](https://trac.ffmpeg.org/wiki/Concatenate#demuxer) (use if your inputs are all the same format, etc and you want to [stream copy](http://ffmpeg.org/ffmpeg.html#Stream-copy)) or [concat filter](http://ffmpeg.org/ffmpeg-filters.html#concat) (use if you want to re-encode, and/or if your inputs do not have same formats, timebase, etc). – llogan Dec 04 '14 at 01:09
  • Hey thank you very much. I have just tested multiple mp4 files from my GoPro using mp4box. I am amazed how well this worked. They were combined flawlessly. – daniel Jan 24 '18 at 14:59
  • @llogan why no answer then – Furkan Gözükara Mar 23 '21 at 07:54
  • @MonsterMMORPG Added an [answer](https://stackoverflow.com/a/66767966/). – llogan Mar 23 '21 at 17:11
6

You can't concatenate .mp4 files but you can concatenate .mpg files. Try converting both videos to .mpg first using ffmpeg. Then, run a simple linux cat command on both .mpg files to create a combined .mpg file. After that, convert the concatenated .mpg file to .mp4 using ffmpeg. This is sort of a roundabout approach but it works. You can use "named pipes" to reduce the number of commands but the result is the same.

Murphtime
  • 61
  • 1
  • 2
5

concat demuxer: fast and preserves quality

If all of the inputs have the same attributes, formats, and number of video/audio streams, then you can use the concat demuxer. This can allow you to concatenate without needing to re-encode, so it is fast and preserves quality.

  1. Make input.txt containing the following:

    file 'input1.mp4'
    file 'input2.mp4'
    
  2. Concatenate

    ffmpeg -f concat -i input.txt -c copy output.mp4
    

    If you get error Unsafe file name add the -safe 0 concat demuxer input option (before -i).

If your inputs do not match

If your inputs are different or are arbitrary then either:

llogan
  • 121,796
  • 28
  • 232
  • 243