Combine two vides with ffmpeg
Asked Answered
R

6

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?

Rhapsody answered 27/9, 2011 at 10:34 Comment(4)
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?Farmland
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.Bimetallism
both videos are of the same format and i get only the first video in the ouptput file i get.Rhapsody
i want to combine both the videos sequentially.Rhapsody
B
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).

Bimetallism answered 27/9, 2011 at 10:42 Comment(1)
What about the video size, how to convert it to the right size before concatenation?Kirkman
T
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.

Toadflax answered 4/12, 2014 at 16:45 Comment(1)
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 lotGunnery
B
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).

Bimetallism answered 27/9, 2011 at 10:42 Comment(1)
What about the video size, how to convert it to the right size before concatenation?Kirkman
K
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.

Klemens answered 27/12, 2011 at 14:23 Comment(0)
V
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
Veronica answered 3/12, 2014 at 22:41 Comment(4)
You can do this with ffmpeg using the concat demuxer (use if your inputs are all the same format, etc and you want to stream copy) or concat filter (use if you want to re-encode, and/or if your inputs do not have same formats, timebase, etc).Northnorthwest
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.Slapbang
@Northnorthwest why no answer thenGearalt
@MonsterMMORPG Added an answer.Northnorthwest
N
6

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:

Northnorthwest answered 23/3, 2021 at 17:10 Comment(0)
G
0

This may not be the best solution, since it creates temporary file. But I would like to provide people with a straightforward answer as an alternative:

#!/usr/bin/env bash
# Synopsis: Combine any input videos to a mp4 file.
# Usage: ffmpeg-combine-videos input_video1 input input_video2 ...

n=0

rm -f __input*
> __input.list

for i in "$@";do
    I=__input${n}.ts
    ffmpeg -i "$i" -c:v h264 -b:v ${VIDEO_BITRATE:-400k} -c:a libmp3lame -f mpegts "$I"
    echo "file $I" >> __input.list
    n=$((n+1))
done

ffmpeg -f concat -safe 0 -i __input.list -c copy combined.mp4

rm -f __input*
Guffey answered 27/1 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.