1

I am trying to merge an audio file with video and its existing audio, only I want to add the new audio, not at the beginning, but at the 7th second of the video.

Something like this:

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

Here is the error I get:

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!

This command works when I replace amerge with amix, but when it seems that only the LAST audio has full volume, so I figured I need amerge so that all audio files I add to the video keep their original volume

Where am I going wrong with this?

2 Answers2

1

The error says: "Consider inserting the (a)format filter near their input or output."

We may try adding aformat filters to the 2 inputs, and to the output of amerge filter.
The relevant (missing) parameter of aformat is channel_layouts.

Example:
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


Note:
The solution is not going to work in all the cases.
There may be cases where we have to insert silent audio stream for filling the gaps.
There may be issues when the input audio layout is not mono.


Update:

Since the input to adelay is mono, we don't suppose to use adelay=7000|7000 (setting delay for left and right channels), we better replace it with 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

We may also move adelay to be after amerge:

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

Rotem
  • 3,346
0

It seems that if I use amix with specific volume (e.g. amix=inputs=2,volume=3) it fixes my issue