Astha
Astha

Reputation: 1734

Combine two vides with ffmpeg

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?

Upvotes: 19

Views: 43164

Answers (6)

Miao ZhiCheng
Miao ZhiCheng

Reputation: 657

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*

Upvotes: 0

llogan
llogan

Reputation: 133693

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:

Upvotes: 6

mpenkov
mpenkov

Reputation: 21896

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).

Upvotes: 9

Marcel Korpel
Marcel Korpel

Reputation: 21763

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

Upvotes: 6

user1592546
user1592546

Reputation: 1480

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.

Upvotes: 21

Murphtime
Murphtime

Reputation: 61

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.

Upvotes: 6

Related Questions