FFMPEG 音频/视频在剪切和连接后不同步,即使转码后也是如此

FFMPEG 音频/视频在剪切和连接后不同步,即使转码后也是如此

我正在尝试从一组视频中剪切并将它们与 concat 解复用器连接在一起。

但是,每次输出时音频都与视频不同步。随着视频的播放,音频似乎进一步不同步。有趣的是,如果我点击播放器上的进度条在视频中寻找另一个时间,音频会与视频同步,但随后又逐渐不同步。在播放器中寻找新的时间似乎会重置音频/视频。就像它们以不同的速率播放一样。我在 Quicktime 和 VLC 播放器中都遇到了这种情况。

对于每个视频,我都会对其进行解码、剪辑,然后将其连同音频一起编码为 4k 分辨率、25 fps:

ffmpeg -ss 0.5 -t 0.5 -i input_video1.mp4 -r 25 -vf scale=3840:2160 output_video1.mp4

然后我把每一个视频用 concat 解复用器连接在一起:

ffmpeg -f concat -safe 0 -i cut_videos.txt -c copy -y output.mp4

我对每个视频都进行了剪辑(大约 0.5 秒)

我正在使用 Python 的子进程来自动剪切和连接视频。

我不确定这是否因为修剪或连接步骤而发生,但是当我播放中间剪切的视频文件(output_video1.mp4在上面的命令中)时,在视频开始时音频进入之前似乎有一些静音。

当我连接视频时,有时会收到很多这些警告,但是即使我没有收到这些警告,音频仍然不同步:

[mp4 @ 0000021a252ce080] Non-monotonous DTS in output stream 0:1; previous: 51792, current: 50009; changing to 51793. This may result in incorrect timestamps in the output file.

这个帖子,这似乎是剪切视频及其时间戳的问题。帖子中提出的解决方案是解码、剪切然后编码视频,但我已经在这样做了。

如何确保音频和视频同步?我的转码是否不正确?这似乎是我在网上找到的唯一解决方案,但它似乎不起作用。

更新:

我从中汲取灵感这个帖子并使用以下方法分离音频和视频output_video1.mp4

ffmpeg -i output_video1.mp4 -acodec copy -vn video.mp4

ffmpeg -i output_video1.mp4 -vcodec copy -an audio.mp4

video.mp4然后我比较了和的持续时间,audio.mp4结果分别为 0.57 秒和 0.52 秒。由于视频较长,这解释了为什么视频中有一段静音时间。然后帖子建议转码是解决方案,但是从上面的代码中可以看出,这对我来说不起作用。

Trim 命令的输出日志示例

  built with Apple LLVM version 10.0.0 (clang-1000.11.45.5)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.2 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-13.0.1.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-13.0.1.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --disable-libjack --disable-indev=jack
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input_video1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:00:04.06, start: 0.000000, bitrate: 14266 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 3840x2160, 14268 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
    Metadata:
      handler_name    : Core Media Video
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 94 kb/s (default)
    Metadata:
      handler_name    : Core Media Audio
File 'output_video1.mp4' already exists. Overwrite ? [y/N] y
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0x7fcae4001e00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x7fcae4001e00] profile High, level 5.1
[libx264 @ 0x7fcae4001e00] 264 - core 155 r2917 0a84d98 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'output_video1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
    Stream #0:0(und): Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 3840x2160, q=-1--1, 25 fps, 12800 tbn, 25 tbc (default)
    Metadata:
      handler_name    : Core Media Video
      encoder         : Lavc58.54.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 69 kb/s (default)
    Metadata:
      handler_name    : Core Media Audio
      encoder         : Lavc58.54.100 aac
frame=   14 fps=7.0 q=-1.0 Lsize=     928kB time=00:00:00.51 bitrate=14884.2kbits/s dup=0 drop=1 speed=0.255x    
video:922kB audio:5kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.194501%
[libx264 @ 0x7fcae4001e00] frame I:1     Avg QP:21.06  size:228519
[libx264 @ 0x7fcae4001e00] frame P:4     Avg QP:22.03  size: 85228
[libx264 @ 0x7fcae4001e00] frame B:9     Avg QP:22.88  size: 41537
[libx264 @ 0x7fcae4001e00] consecutive B-frames: 14.3%  0.0%  0.0% 85.7%
[libx264 @ 0x7fcae4001e00] mb I  I16..4: 27.6% 64.3%  8.1%
[libx264 @ 0x7fcae4001e00] mb P  I16..4:  9.1% 10.7%  0.2%  P16..4: 48.5%  7.3%  3.9%  0.0%  0.0%    skip:20.2%
[libx264 @ 0x7fcae4001e00] mb B  I16..4:  1.1%  1.0%  0.0%  B16..8: 44.5%  2.9%  0.2%  direct: 8.3%  skip:42.0%  L0:45.6% L1:53.2% BI: 1.2%
[libx264 @ 0x7fcae4001e00] 8x8 transform intra:58.2% inter:93.4%
[libx264 @ 0x7fcae4001e00] coded y,uvDC,uvAC intra: 31.4% 62.2% 5.2% inter: 11.4% 30.9% 0.0%
[libx264 @ 0x7fcae4001e00] i16 v,h,dc,p: 15% 52% 12% 21%
[libx264 @ 0x7fcae4001e00] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 19% 33% 32%  2%  2%  2%  4%  2%  4%
[libx264 @ 0x7fcae4001e00] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 20% 39%  9%  3%  4%  4% 12%  3%  4%
[libx264 @ 0x7fcae4001e00] i8c dc,h,v,p: 43% 36% 18%  3%
[libx264 @ 0x7fcae4001e00] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x7fcae4001e00] ref P L0: 69.3%  8.0% 14.8%  7.9%
[libx264 @ 0x7fcae4001e00] ref B L0: 88.1%  9.2%  2.6%
[libx264 @ 0x7fcae4001e00] ref B L1: 90.2%  9.8%
[libx264 @ 0x7fcae4001e00] kb/s:13475.29
[aac @ 0x7fcae4012400] Qavg: 125.000```

相关内容