ffmpeg merge audio with video and lower volume

5,644

Use

ffmpeg \
-i output.mp4 \
-i music.mp3 \
-filter_complex "[1:a]volume=0.15,apad[A];[0:a][A]amerge[out]" \
-c:v copy -map 0:v -map [out] -y output-final.mp4
Share:
5,644

Related videos on Youtube

730wavy
Author by

730wavy

Freelance Graphic Designer and Web Developer.

Updated on September 18, 2022

Comments

  • 730wavy
    730wavy over 1 year

    I am trying to merge an audio mp3 file with a video mp4 file. I want the audio file's volume to be turned down a little bit so that it is background music for the video clip.

    This is how my code looks now (I tried adapting from a previous code merging 2 mp3's) --

    ffmpeg \
    -i output.mp4 \ 
    -i music.mp3 \ 
    -filter_complex "[0:a]volume=0.15[A];[1:a][A]amerge[out]" \
    -map [out] -y output-final.mp4 2>&1
    

    EDIT

    There seems to be an issue on iphone when encoding mp4 videos using filter_complex with amerge.

    Console output -

    ffmpeg version N-90767-g768c077-syslint Copyright (c) 2000-2018 the FFmpeg developers
      built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-16)
      configuration: --prefix=/usr/local/cpffmpeg --enable-shared --enable-nonfree --enable-gpl --enable-pthreads --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --extra-cflags=-I/usr/local/cpffmpeg/include --extra-ldflags=-L/usr/local/cpffmpeg/lib --enable-version3 --extra-version=syslint
      libavutil      56. 15.100 / 56. 15.100
      libavcodec     58. 19.100 / 58. 19.100
      libavformat    58. 13.100 / 58. 13.100
      libavdevice    58.  4.100 / 58.  4.100
      libavfilter     7. 18.100 /  7. 18.100
      libswscale      5.  2.100 /  5.  2.100
      libswresample   3.  2.100 /  3.  2.100
      libpostproc    55.  2.100 / 55.  2.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/path/to/output-4786-second.mp4':
      Metadata:
        major_brand     : isom
        minor_version   : 512
        compatible_brands: isomiso2avc1mp41
        encoder         : Lavf58.13.100
      Duration: 00:00:13.58, start: 0.000000, bitrate: 798 kb/s
        Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 767 kb/s, 24 fps, 24 tbr, 12288 tbn, 48 tbc (default)
        Metadata:
          handler_name    : VideoHandler
        Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 22050 Hz, mono, fltp, 29 kb/s (default)
        Metadata:
          handler_name    : SoundHandler
    Input #1, mp3, from '/path/to/buddy.mp3':
      Duration: 00:02:02.12, start: 0.025057, bitrate: 112 kb/s
        Stream #1:0: Audio: mp3, 44100 Hz, stereo, fltp, 112 kb/s
        Metadata:
          encoder         : LAME3.99r
    Stream mapping:
      Stream #0:1 (aac) -> amerge:in0 (graph 0)
      Stream #1:0 (mp3float) -> volume (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 @ 0x1cae600] using SAR=1/1
    [libx264 @ 0x1cae600] using cpu capabilities: none!
    [libx264 @ 0x1cae600] profile Constrained Baseline, level 3.1
    [libx264 @ 0x1cae600] 264 - core 155 r2901 7d0ff22 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=1 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=22 lookahead_threads=3 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=24 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
    [Parsed_amerge_2 @ 0x2ed3780] No channel layout for input 1
    Output #0, mp4, to '/path/to/output-4786.mp4':
      Metadata:
        major_brand     : isom
        minor_version   : 512
        compatible_brands: isomiso2avc1mp41
        encoder         : Lavf58.13.100
        Stream #0:0(und): Video: h264 (libx264) (avc1 / 0x31637661), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 24 fps, 12288 tbn, 24 tbc (default)
        Metadata:
          handler_name    : VideoHandler
          encoder         : Lavc58.19.100 libx264
        Side data:
          cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
        Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 22050 Hz, 3.0, fltp, 197 kb/s (default)
        Metadata:
          encoder         : Lavc58.19.100 aac
    frame=   82 fps=0.0 q=12.0 size=       0kB time=00:00:02.50 bitrate=   0.2kbits/s speed=4.97x    
    frame=  157 fps=156 q=18.0 size=     512kB time=00:00:05.57 bitrate= 752.7kbits/s speed=5.53x    
    frame=  210 fps=139 q=13.0 size=    1280kB time=00:00:07.80 bitrate=1344.0kbits/s speed=5.18x    
    frame=  273 fps=136 q=12.0 size=    1280kB time=00:00:10.40 bitrate=1008.0kbits/s speed=5.18x    
    frame=  326 fps=145 q=-1.0 Lsize=    1607kB time=00:00:13.54 bitrate= 972.3kbits/s speed=6.04x    
    video:1290kB audio:310kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.406112%
    [libx264 @ 0x1cae600] frame I:2     Avg QP:15.50  size: 21673
    [libx264 @ 0x1cae600] frame P:324   Avg QP:13.50  size:  3942
    [libx264 @ 0x1cae600] mb I  I16..4: 100.0%  0.0%  0.0%
    [libx264 @ 0x1cae600] mb P  I16..4:  1.6%  0.0%  0.0%  P16..4: 12.7%  0.0%  0.0%  0.0%  0.0%    skip:85.7%
    [libx264 @ 0x1cae600] coded y,uvDC,uvAC intra: 25.5% 29.6% 12.4% inter: 4.2% 8.3% 1.4%
    [libx264 @ 0x1cae600] i16 v,h,dc,p: 62% 18%  9% 11%
    [libx264 @ 0x1cae600] i8c dc,h,v,p: 68% 16% 11%  5%
    [libx264 @ 0x1cae600] kb/s:777.82
    [aac @ 0x1caf980] Qavg: 4402.419
    

    Most recent command attempted --

    ffmpeg \
    -i '.$thepath.'/output-'.$v_Id.'-second.mp4 \
    -i '.$musicpath .'/'.$music.'.mp3 \
    -filter_complex "[1:a]volume=0.15,apad[A];[0:a][A]amerge[out]" \
    -c:v libx264 -c:a aac -map 0:v -map "[out]" \
    -preset ultrafast -threads 0 -pix_fmt yuv420p -y '.$thepath.'/output-'.$v_Id.'.mp4 2>&1
    
  • 730wavy
    730wavy over 5 years
    im getting this error -- sh: line 3: -filter_complex: command not found
  • Gyan
    Gyan over 5 years
    Removed trailing whitespace
  • 730wavy
    730wavy over 5 years
    that wasnt the issue
  • Gyan
    Gyan over 5 years
    Just put it all on one line and execute
  • 730wavy
    730wavy over 5 years
    i ran into a problem on iphone safari. The mp4 videos wont play on it but plays every where else. I've tried adding different combinations of the options -vprofile baseline -level 3.0 -pix_fmt yuv420p -movflags +faststart -shortest but nothing works. Any ideas?
  • Gyan
    Gyan over 5 years
    With -c:v copy, none of those options will have an effect. Drop it and check.
  • 730wavy
    730wavy over 5 years
    same issue is occurring.
  • Gyan
    Gyan over 5 years
    Share full log.
  • 730wavy
    730wavy over 5 years
    Ok i updated my question with the log
  • Gyan
    Gyan over 5 years
    Add -profile:v baseline -ac 2 to your current command.
  • 730wavy
    730wavy over 5 years
    yes i think that did the trick. Im going to test some more but so far it seems to work now. Thank you.