Back

使用ffmpeg 组合两端视频 (combine videos using ffmpeg )

发布时间: 2013-05-30 02:05:00

see: http://ffmpeg.org/trac/ffmpeg/wiki/How%20to%20concatenate%20(join,%20merge)%20media%20files

2014-06-23 更新: 更快的办法:

1: create a file containing x files: 
file '/workspace/videos/zhangong-new-1.mp4'
file '/workspace/videos/zhangong-new-2.mp4'

( 快速从开头获取到某部位的视频) : 
$ ffmpeg -i zhangong-0623.mp4 -t 00:00:10 -c copy zhangong-header.mp4

2. $ ffmpeg -f concat  -i zhangong.txt -c copy zhangong-0623.mp4

简而言之,对于两个mp4文件,先把它们转换成mpegts 文件,再组合成一个mp4:

ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts
ffmpeg -i input2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2.ts
ffmpeg -i "concat:intermediate1.ts|intermediate2.ts" -c copy qiankunbu_total.mp4  ( 注意这里不要有  -bsf参数) 

While the demuxer works at the stream level, the concat protocol works at the file level. Certain files (mpg and mpeg transport streams, possibly others) can be concatenated. This is analogous to using cat on UNIX-like systems or copy on Windows. In general, the demuxer is the better option.

Instructions

ffmpeg -i "concat:input1.mpg|input2.mpg|input3.mpg" -c copy output.mpg
If you have MP4 files, these could be losslessly concatenated by first transcoding them to mpeg transport streams. With h.264 video and AAC audio, the following can be used:

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

Back