ffmpeg 在特定时间将音频文件与视频及其音频合并

ffmpeg 在特定时间将音频文件与视频及其音频合并

我正在尝试将音频文件与视频及其现有音频合并,但我只想添加新音频,不是在开头,而是在视频的第 7 秒。

像这样:

ffmpeg -y -i vide.mp4 -i audio.wav -filter_complex "[1:a] adelay=7000|7000 [tmp];[0:a][tmp] amerge=inputs=2 [audio_out]" -map 0:v -map [audio_out] output.mp4

这是我收到的错误:

ffmpeg version 4.2.7-0ubuntu0.1 Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
  configuration: --prefix=/usr --extra-version=0ubuntu0.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-nvenc --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
  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 'output_0.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:00:14.03, start: 0.000000, bitrate: 1572 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 640x624, 1549 kb/s, 60.01 fps, 60 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 32000 Hz, mono, fltp, 61 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Guessed Channel Layout for Input Stream #1.0 : mono
Input #1, wav, from 'audio/not_deep_enough_squat.wav':
  Duration: 00:00:03.16, bitrate: 512 kb/s
    Stream #1:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 32000 Hz, mono, s16, 512 kb/s
Stream mapping:
  Stream #0:1 (aac) -> amerge:in0 (graph 0)
  Stream #1:0 (pcm_s16le) -> adelay (graph 0)
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  amerge (graph 0) -> Stream #0:1 (aac)
Press [q] to stop, [?] for help
[libx264 @ 0x5606959de1c0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x5606959de1c0] profile High, level 3.1
[libx264 @ 0x5606959de1c0] 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=9 lookahead_threads=1 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
[Parsed_amerge_1 @ 0x560695ea69c0] No channel layout for input 1
[Parsed_amerge_1 @ 0x560695ea69c0] No channel layout for input 2
[AVFilterGraph @ 0x560695ea5000] The following filters could not choose their formats: Parsed_amerge_1
Consider inserting the (a)format filter near their input or output.
Error reinitializing filters!
Failed to inject frame into filter network: Input/output error
Error while processing the decoded data for stream #1:0
Conversion failed!

amerge当我用替换时,此命令有效amix,但似乎只有最后一个音频具有最大音量,所以我认为我需要amerge这样,我添加到视频中的所有音频文件都保持其原始音量

我哪里做错了?

答案1

错误说:“考虑在其输入或输出附近插入(a)格式过滤器。”

我们可以尝试添加格式过滤器连接到 2 个输入,并连接到过滤器的输出amerge
的相关(缺失)参数aformatchannel_layouts

例子:
ffmpeg -y -i vide.mp4 -i audio.wav -filter_complex "[0:a]aformat=channel_layouts=mono[0a];[1:a]adelay=7000|7000,aformat=channel_layouts=mono[tmp];[0a][tmp]amerge=inputs=2,aformat=channel_layouts=stereo[audio_out]" -map 0:v -map [audio_out] output.mp4


注意:
该解决方案并非在所有情况下都有效。在
某些情况下,我们必须插入静音音频流来填补空白。
当输入音频布局不是单声道时,可能会出现问题。


更新:

由于输入adelay是单声道,我们不应该使用adelay=7000|7000(设置左右声道的延迟),我们最好将其替换为adelay=7000:all=1

ffmpeg -y -i vide.mp4 -i audio.wav -filter_complex "[0:a]aformat=channel_layouts=mono[0a];[1:a]adelay=7000:all=1,aformat=channel_layouts=mono[tmp];[0a][tmp]amerge=inputs=2,aformat=channel_layouts=stereo[audio_out]" -map 0:v -map [audio_out] output.mp4

我们也可以移动adelay到以下位置amerge

ffmpeg -y -i vide.mp4 -i audio.wav -filter_complex "[0:a][1:a]amerge=inputs=2,adelay=0|7000" output.mp4

答案2

似乎如果我使用具有特定音量的 amix(例如 amix=inputs=2,volume=3),它就能解决我的问题

相关内容