开发者

Combining two video from ffmpeg

开发者 https://www.devze.com 2023-04-08 11:47 出处:网络
I want to combine two mp4 videos to form a single mp4 video using ffmpeg. what i tried开发者_Go百科 so far is

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

what i tried开发者_Go百科 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.


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.


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


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.


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


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:

  • Use the concat filter. See How to concatenate videos in ffmpeg with different attributes? The requires re-encoding, but is the better choice when your inputs are random, mixed, unpredictable, or arbitrary.

  • Or re-encode the inputs that do not match the majority of inputs, then use the concat demuxer. There are many examples on this site showing how to do this.

  • Also see FFmpeg Wiki: Concatenate.

0

精彩评论

暂无评论...
验证码 换一张
取 消