我正在尝试使用 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。

另请参阅


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!