ffmpeg:如何在播放叠加视频时同步音频

ffmpeg:如何在播放叠加视频时同步音频

我正在尝试合并两个视频:

  • 基础视频
  • 较短的视频叠加在顶部,从基础视频的 2 秒处开始

当我执行此操作时,叠加视频的音频会随着基础视频的开始而立即开始,而不是在显示叠加视频时开始。

我尝试了几十种 setpts、asetpts、adelay 等的排列组合,但无法在显示叠加视频时播放叠加视频的音频。我尝试在这个网站上寻找相关问题,但没有找到一个可以回答这个问题的。

这是:

这是我的完整命令行和输出,我试图将覆盖视频放在基础视频的中心,从视频开始两秒。

ffmpeg -y -i base.mp4 -i overlay.mp4 -c:v libx264 -c:a libfdk_aac -filter_complex " [0:v] setpts=PTS-STARTPTS [b1] ; [1:v] setpts=PTS-STARTPTS+2/TB [b2] ; [b1] [b2] overlay=x=320:y=120:eof_action=pass [outv] ; [1:a] adelay=2|2 [outa] " -map "[outv"] -map "[outa]" combined.mp4
ffmpeg version N-81978-g03d6d5f Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
  configuration: --enable-gpl --enable-libx264 --enable-nonfree --enable-libfdk-aac
  libavutil      55. 32.100 / 55. 32.100
  libavcodec     57. 61.102 / 57. 61.102
  libavformat    57. 51.107 / 57. 51.107
  libavdevice    57.  0.102 / 57.  0.102
  libavfilter     6. 63.100 /  6. 63.100
  libswscale      4.  1.100 /  4.  1.100
  libswresample   2.  2.100 /  2.  2.100
  libpostproc    54.  0.100 / 54.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'base.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.51.107
  Duration: 00:00:10.05, start: 0.000000, bitrate: 116 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 10 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 96 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'overlay.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.51.107
  Duration: 00:00:05.05, start: 0.000000, bitrate: 1821 kb/s
    Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x480 [SAR 1:1 DAR 4:3], 1728 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #1:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 96 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
[libx264 @ 0x3076e40] using SAR=1/1
[libx264 @ 0x3076e40] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX AVX2 FMA3 LZCNT BMI2
[libx264 @ 0x3076e40] profile High, level 3.1
[libx264 @ 0x3076e40] 264 - core 142 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - 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 'combined.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.51.107
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 29.97 fps, 30k tbn, 29.97 tbc (default)
    Metadata:
      encoder         : Lavc57.61.102 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: aac (libfdk_aac) ([64][0][0][0] / 0x0040), 44100 Hz, mono, s16, 96 kb/s (default)
    Metadata:
      encoder         : Lavc57.61.102 libfdk_aac
Stream mapping:
  Stream #0:0 (h264) -> setpts
  Stream #1:0 (h264) -> setpts
  Stream #1:1 (aac) -> adelay
  overlay -> Stream #0:0 (libx264)
  adelay -> Stream #0:1 (libfdk_aac)
Press [q] to stop, [?] for help
frame=  300 fps=188 q=-1.0 Lsize=     611kB time=00:00:09.90 bitrate= 505.4kbits/s speed=6.22x    
video:543kB audio:60kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.399169%
[libx264 @ 0x3076e40] frame I:2     Avg QP:10.60  size: 13404
[libx264 @ 0x3076e40] frame P:75    Avg QP:16.09  size:  4471
[libx264 @ 0x3076e40] frame B:223   Avg QP:19.79  size:   868
[libx264 @ 0x3076e40] consecutive B-frames:  0.7%  0.0%  2.0% 97.3%
[libx264 @ 0x3076e40] mb I  I16..4: 84.4%  8.1%  7.5%
[libx264 @ 0x3076e40] mb P  I16..4:  1.6%  0.3%  0.1%  P16..4:  6.6%  4.7%  2.4%  0.0%  0.0%    skip:84.2%
[libx264 @ 0x3076e40] mb B  I16..4:  0.0%  0.0%  0.0%  B16..8:  6.9%  1.2%  0.2%  direct: 0.3%  skip:91.3%  L0:43.4% L1:49.6% BI: 7.0%
[libx264 @ 0x3076e40] 8x8 transform intra:12.1% inter:54.7%
[libx264 @ 0x3076e40] coded y,uvDC,uvAC intra: 12.6% 15.3% 2.2% inter: 2.1% 1.8% 0.0%
[libx264 @ 0x3076e40] i16 v,h,dc,p: 94%  3%  2%  1%
[libx264 @ 0x3076e40] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 17% 22% 30%  3%  6%  6%  5%  3%  7%
[libx264 @ 0x3076e40] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 23% 25% 13%  4%  7%  8%  7%  5%  7%
[libx264 @ 0x3076e40] i8c dc,h,v,p: 88%  6%  6%  0%
[libx264 @ 0x3076e40] Weighted P-Frames: Y:2.7% UV:1.3%
[libx264 @ 0x3076e40] ref P L0: 63.2% 14.8% 18.0%  3.9%  0.1%
[libx264 @ 0x3076e40] ref B L0: 94.4%  4.8%  0.8%
[libx264 @ 0x3076e40] ref B L1: 96.9%  3.1%
[libx264 @ 0x3076e40] kb/s:444.04

答案1

adelay值必须以毫秒为单位设置。使用2000代替2,一切都将如预期:

ffmpeg -y -i base.mp4 -i overlay.mp4 -c:v libx264 -c:a aac -filter_complex " [0:v] setpts=PTS-STARTPTS [b1] ; [1:v] setpts=PTS-STARTPTS+2/TB [b2] ; [b1] [b2] overlay=x=320:y=120:eof_action=pass [outv] ; [1:a] adelay=2000|2000 [outa] " -map "[outv"] -map "[outa]" combined.mp4

相关内容