如何在 FFmpeg 中用静音填充部分/损坏的 TS 流音轨?

如何在 FFmpeg 中用静音填充部分/损坏的 TS 流音轨?

我有一些来自电视录制的原始 TS 流文件。这些文件的长度大约为 1 小时,大小为 10GB。这些文件可能分为 4 (5) 种

  1. 完整视频,完整音频
  2. 完整视频,完全无音频流
  3. 完整视频,音频流提前结束
  4. 完整视频,音频流开始较晚
  5. 完整视频、音频流中断 - 尚未直接遇到过

我想“无损”地修复这些问题,以便进一步显示或编辑。

当使用这些时,#4让我头疼不已。音频流延迟太久,如果不增加探测大小和分析持续时间,FFMPEG 甚至无法接收它。

我现在用来批量修复所有这些问题的完整命令是

ffmpeg.exe -probesize 2147483647G -analyzeduration 2147483647G -err_detect ignore_err -i INPUT.ts -f lavfi -i anullsrc -shortest -vcodec copy -acodec flac -compression_level 12 -max_muxing_queue_size 2147483647 OUTPUT.mkv

(anullsrc 用于在没有音频流和 max_muxing_queue_size 的视频上提供替代的静音音轨,否则 ffmpeg 会出错)

案例 4 的结果是视频可以正常工作,并且在 ffprobe 中显示的视频和音频长度相等。在 Audacity 中提取或查看音轨时,前 20 分钟被报告为丢失的音频帧,实际音频长度约为 40 分钟。

我正在寻找一种方法来使用静音/anullsrc正确填充丢失或缺失的音频。

我到目前为止尝试的是附加

-af aresample=async=1

因为我听说它会用静音填充流中缺失的音频

-filter_complex amix=inputs=2:normalize=false

希望将后期音频与来自 anullsrc 的持续静音混合会产生等长音频流。

到目前为止的结果始终是相同的音轨,在前 20 分钟内丢失音频帧。

(虽然看似已经修复并且音轨至少现在已被识别,但即使在进一步有损重新编码时仍然会出现丢失音频帧的情况。)

使用给定命令记录编码#4

[mpegts @ 000001f322d71a00] Failed to allocate buffers for seekback
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[h264 @ 000001f322e17cc0] non-existing PPS 0 referenced
    Last message repeated 1 times
[h264 @ 000001f322e17cc0] decode_slice_header error
[h264 @ 000001f322e17cc0] no frame!
[mpegts @ 000001f322d71a00] Packet corrupt (stream = 0, dts = 6338804940).
[mpegts @ 000001f322d71a00] DTS discontinuity in stream 0: packet 28707 with DTS 6338804940, packet 28708 with DTS 8590190192
[mpegts @ 000001f322d71a00] Packet corrupt (stream = 0, dts = 15573600).
[mpegts @ 000001f322d71a00] Packet corrupt (stream = 0, dts = 103910400).
Input #0, mpegts, from 'INPUT.ts':
  Duration: 07:56:50.91, start: 69474.266000, bitrate: 2649 kb/s
  Program 490
  Stream #0:0[0x21]: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuv420p(progressive), 1920x1080, 25 fps, 50 tbr, 90k tbn
  Stream #0:1[0x22]: Audio: mp2 ([4][0][0][0] / 0x0004), 48000 Hz, stereo, fltp, 192 kb/s
Input #1, lavfi, from 'anullsrc':
  Duration: N/A, start: 0.000000, bitrate: 705 kb/s
  Stream #1:0: Audio: pcm_u8, 44100 Hz, stereo, u8, 705 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (mp2 (native) -> flac (native))
Press [q] to stop, [?] for help
Output #0, matroska, to 'OUTPUT.mkv':
  Metadata:
    encoder         : Lavf60.20.100
  Stream #0:0: Video: h264 (Constrained Baseline) (H264 / 0x34363248), yuv420p(progressive), 1920x1080, q=2-31, 25 fps, 50 tbr, 1k tbn
  Stream #0:1: Audio: flac ([172][241][0][0] / 0xF1AC), 48000 Hz, stereo, s16, 128 kb/s
      Metadata:
        encoder         : Lavc60.36.100 flac
[mpegts @ 000001f322d71a00] Packet corrupt (stream = 0, dts = 6338804940).
[in#0/mpegts @ 000001f322d7e240] corrupt input packet in stream 0
[vist#0:0/h264 @ 000001f454557e40] timestamp discontinuity (stream id=33): -25014584689, new offset= 7000
[aist#0:1/mp2 @ 000001f4bf2a3040] timestamp discontinuity (stream id=34): 25014584689, new offset= -25014577689
[vist#0:0/h264 @ 000001f454557e40] timestamp discontinuity (stream id=33): -25014591689, new offset= 14000
[aist#0:1/mp2 @ 000001f4bf2a3040] timestamp discontinuity (stream id=34): 25014591689, new offset= -25014577689
[mpegts @ 000001f322d71a00] Packet corrupt (stream = 0, dts = 15573600).
[in#0/mpegts @ 000001f322d7e240] corrupt input packet in stream 0
[mpegts @ 000001f322d71a00] Packet corrupt (stream = 0, dts = 103910400).
[in#0/mpegts @ 000001f322d7e240] corrupt input packet in stream 0
[out#0/matroska @ 000001f4434f07c0] video:8958881kB audio:189875kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.011034%
size= 9149766kB time=00:59:56.33 bitrate=20842.0kbits/s speed=58.9x

编辑:直接用 aresample 指定起点 0 可以解决延迟启动音轨的问题。

-af aresample=async=1:first_pts=0

谢谢@Gyan提供正确的解决方案

编辑2:遗憾的是,这仍然不适用于提前结束的音轨(#3)。为此,必须额外调用 apad 过滤器

如果有人遇到同样的问题,请完成命令

ffmpeg.exe -probesize 2147483647G -analyzeduration 2147483647G -err_detect ignore_err -i INPUT -f lavfi -i anullsrc -shortest -vcodec copy -acodec flac -compression_level 12 -af aresample=async=1:first_pts=0,apad -max_muxing_queue_size 2147483647 OUTPUT.mkv

相关内容