使用 ffmpeg 修剪视频并连接 - 出现“输出流中非单调 DTS”错误

使用 ffmpeg 修剪视频并连接 - 出现“输出流中非单调 DTS”错误

我有两个视频,我需要删除两个视频的两端,只留下中间部分,然后将中间部分连接在一起。两个视频的来源完全相同 - 相同的 FPS 和相同的编解码器。

我正在尝试做的事情:

ffmpeg -i 0.mp4 -c:v h264_nvenc -preset fast -rc vbr -qmin 1 -qmax 1 -filter_complex "[0:v]trim=start=01.00:duration=03.00,setpts=PTS-STARTPTS[vmiddle]" -map [vmiddle] middle_0.mp4
ffmpeg -i 1.mp4 -c:v h264_nvenc -preset fast -rc vbr -qmin 1 -qmax 1 -filter_complex "[0:v]trim=start=01.00:duration=03.00,setpts=PTS-STARTPTS[vmiddle]" -map [vmiddle] middle_1.mp4

这两个命令仅剪切每个视频的中间部分,从 01.00 开始到 04.00 结束。

然后我连接视频:

ffmpeg -y -f concat -i concat.txt -c copy concat.mp4

concat.txt 文件显然包含:

file 'middle0.mp4'
file 'middle1.mp4'

但这不起作用。输出视频concat.mp4包含奇怪的故障,我在控制台中收到很多错误:

[mov,mp4,m4a,3gp,3g2,mj2 @ 00000000025ead60] Auto-inserting h264_mp4toannexb bitstream filter
[concat @ 0000000000dd6bc0] DTS 34666 < 45568 out of order
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45568, current: 34666; changing to 45569. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45569, current: 35050; changing to 45570. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45570, current: 35434; changing to 45571. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45571, current: 35818; changing to 45572. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45572, current: 36202; changing to 45573. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45573, current: 36586; changing to 45574. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45574, current: 36970; changing to 45575. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45575, current: 37354; changing to 45576. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45576, current: 37738; changing to 45577. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45577, current: 38122; changing to 45578. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45578, current: 38506; changing to 45579. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45579, current: 38890; changing to 45580. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45580, current: 39274; changing to 45581. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45581, current: 39658; changing to 45582. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45582, current: 40042; changing to 45583. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45583, current: 40426; changing to 45584. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45584, current: 40810; changing to 45585. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45585, current: 41194; changing to 45586. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45586, current: 41578; changing to 45587. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45587, current: 41962; changing to 45588. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45588, current: 42346; changing to 45589. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45589, current: 42730; changing to 45590. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45590, current: 43114; changing to 45591. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45591, current: 43498; changing to 45592. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45592, current: 43882; changing to 45593. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45593, current: 44266; changing to 45594. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45594, current: 44650; changing to 45595. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45595, current: 45034; changing to 45596. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45596, current: 45418; changing to 45597. This may result in incorrect timestamps in the output file.

注意,我已经设置了演示时间戳,setpts=PTS-STARTPTS因此每个视频都从 00.00 开始。但如何修复这个不单调的 DTS?我真的被难住了,请帮忙。

我知道我可以通过连接期间重新编码视频来解决这个问题,但我真的负担不起。这一步是长流程的一部分,时间至关重要,我必须使用复制编解码器来完成。

添加:

要求ffprobe输出两个视频。我对输出了解不多ffprobe,您能解释一下发生了什么吗?为什么会有差异?这是什么意思?(第一个视频中有 512 步,第二个视频中有 384 步)

Middle0(第一个视频)

Middle1(第二个视频)

答案1

使用

ffmpeg -i 0.mp4 -c:v h264_nvenc -preset fast -rc vbr -qmin 1 -qmax 1 -filter_complex "[0:v]trim=start=01.00:duration=03.00,setpts=PTS-STARTPTS[vmiddle]" -video_track_timescale 18000 -map [vmiddle] middle_0.mp4

ffprobe 读数中的这些数字是显示时间戳,用于通知播放器何时显示每一帧。它们以时间基准表示。因此,时间基准为 1/1200(或时间刻度为 1200)的 PTS 为 600 意味着显示时间为 600 * 1/1200 = 0.5 秒,时间刻度为 1400 的 PTS 为 700 也意味着显示时间为 0.5 秒。现在,当两个时间基准不同的视频合并时,结果将不正确,因为 ffmpeg 将采用第一个视频的时间基准作为最终值。我添加了一个参数来使它们统一。

答案2

按照https://trac.ffmpeg.org/wiki/Concatenate

我遇到了同样的问题。我在 Mac 上运行 ffmpeg 版本 2.6.3。我将源剪辑转换为 .MTS 格式,并添加 -q 0 标签,更新文本文件中的扩展名,并在 ffmpeg 中运行 concat 命令时将输出文件设置为 .MTS。它工作得很好。这是我的工作流程。

ffmpeg -i clip-1.MOV -q 0 clip-1.MTS

ffmpeg -i clip-2.MOV -q 0 clip-2.MTS

更新文本文件(mylist.txt)内容如下:

file clip-1.MTS
file clip-2.MTS

然后运行 ​​concat 命令如下:

ffmpeg -f concat -i mylist.txt -c copy output.MTS

对我有用!您可以将最终输出.MTS 转换为您需要的任何格式。

相关内容