如何使用 FFmpeg 连接两个 MP4 文件? [英] How to concatenate two MP4 files using FFmpeg?

查看:43
本文介绍了如何使用 FFmpeg 连接两个 MP4 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ffmpeg 连接两个 mp4 文件.我需要这是一个自动过程,因此我选择了 ffmpeg.我将这两个文件转换为 .ts 文件,然后将它们连接起来,然后尝试对连接后的 .ts 文件进行编码.这些文件是 h264aac 编码的,我希望尽可能保持原始质量.

I'm trying to concatenate two mp4 files using ffmpeg. I need this to be an automatic process hence why I chose ffmpeg. I'm converting the two files into .ts files and then concatenating them and then trying to encode that concatenated .ts file. The files are h264 and aac encoded and I'm hoping to keep the quality the same or as close to original as possible.

ffmpeg -i part1.mp4 -vcodec copy -vbsf h264_mp4toannexb -acodec copy part1.ts
ffmpeg -i part2.mp4 -vcodec copy -vbsf h264_mp4toannexb -acodec copy part2.ts
cat part1.ts part2.ts > parts.ts
ffmpeg -y -i parts.ts -acodec copy -ar 44100 -ab 96k -coder ac -vbsf h264_mp4toannexb parts.mp4

不幸的是,我在编码过程中从 ffmpeg 收到以下错误消息:

Unfortunately I'm getting the following error message coming back from ffmpeg during encoding:

[h264 @ 0x1012600]sps_id out of range
[h264 @ 0x1012600]non-existing SPS 0 referenced in buffering period
[h264 @ 0x1012600]sps_id out of range
[h264 @ 0x1012600]non-existing SPS 0 referenced in buffering period
[NULL @ 0x101d600]error, non monotone timestamps 13779431 >= 13779431kbits/s    
av_interleaved_write_frame(): Error while opening file

这种情况发生在编码的一半左右,这让我觉得你不能将两个 .ts 文件连接在一起并让它工作.

This happens about half way through encoding which makes me think that you can't concat two .ts files together and have it work.

推荐答案

我最终使用 mpg 作为中间格式并且它有效(注意这是一个危险的例子,-qscale 0 将重新编码视频...)

I ended up using mpg as the intermediate format and it worked (NOTE this is a dangerous example, -qscale 0 will re-encode the video...)

ffmpeg -i 1.mp4 -qscale 0 1.mpg
ffmpeg -i 2.mp4 -qscale 0 2.mpg
cat 1.mpg 2.mpg | ffmpeg -f mpeg -i - -qscale 0 -vcodec mpeg4 output.mp4

这篇关于如何使用 FFmpeg 连接两个 MP4 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆