ffmpeg - 如何使用 FFmpeg 连接两个 MP4 文件?

标签 ffmpeg h.264 mp4

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

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 返回的以下错误消息:

[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 文件连接在一起并使其工作。

最佳答案

FFmpeg 具有三种串联方法:

1。 concat video filter

如果您的输入没有相同的参数(宽度、高度等),或者格式/编解码器不同,或者您想要执行任何过滤,请使用此方法。

请注意,此方法会对所有输入执行重新编码。如果您想避免重新编码,您可以仅对不匹配的输入进行重新编码,以便它们共享相同的编解码器和其他参数,然后使用 concat 解复用器来避免重新编码所有内容。

ffmpeg -i opening.mkv -i episode.mkv -i ending.mkv \
-filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] \
concat=n=3:v=1:a=1 [v] [a]" \
-map "[v]" -map "[a]" output.mkv

2。 concat demuxer

当您想避免重新编码并且您的格式不支持文件级串联(一般用户使用的大多数文件不支持文件级串联)时,请使用此方法。

$ cat mylist.txt
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'
    
$ ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4

对于 Windows:

(echo file 'first file.mp4' & echo file 'second file.mp4' )>list.txt
ffmpeg -safe 0 -f concat -i list.txt -c copy output.mp4

3。 concat protocol

将此方法与支持文件级串联的格式结合使用 (MPEG-1、MPEG-2 PS、DV)。请勿与 MP4 一起使用。

ffmpeg -i "concat:input1|input2" -codec copy output.mkv

由于这些格式的性质以及此方法执行的简单物理连接,此方法不适用于许多格式,包括 MP4。这相当于直接加入文件。

<小时/>

如果对使用哪种方法有疑问,请尝试使用 concat demuxer。

另请参阅

关于ffmpeg - 如何使用 FFmpeg 连接两个 MP4 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45786224/

相关文章:

php - 在 MAMP 上安装 ffmpeg

api - Libav API : Filtering + Transconding

php - 后台处理视频上传,有什么精通PHP的方法?

html - 使用 flash-player 在 HTML 中嵌入 MP4

python - 在python中将元数据从一个mp4文件复制到另一个文件

Android MP4 moov mdat问题

ffmpeg - hls.js - 如何增加预加载的缓冲区大小

OpenCV - 在版本 3.4 中使用 FFMPEG 通过 RTSP 流式传输 H264

ffmpeg - 将 FFmpeg 中的视频编码为 X264 并使其可在 Quicktime 中播放

video - 处理原始视频时如何在 H.264 编解码器中构建 P 帧