FFmpeg add background audio to video but not completely muting the original audio

06
2014-04
  • Xiruki Tepe

    It is merging, but I don't want to remove the original audio of the video. I want to overlay the audio in the background.

    Is there a way to achieve this behavior? I am using this command to merge audio into a video:

    $ ffmpeg -i adele.mp3 -i Ruby\ on\ Rails\ 101-\ What\ is\ Ruby\ on\ Rails_.mp4 -c:v libx264 -c copy -aspect 16:9 -r 30 test.mp4
    ffmpeg version N-60332-ga0d5204 Copyright (c) 2000-2014 the FFmpeg developers
      built on Feb  4 2014 21:31:51 with Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
      configuration: --prefix=/usr/local --enable-gpl --enable-nonfree --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid
      libavutil      52. 63.100 / 52. 63.100
      libavcodec     55. 49.101 / 55. 49.101
      libavformat    55. 29.101 / 55. 29.101
      libavdevice    55.  7.100 / 55.  7.100
      libavfilter     4.  1.102 /  4.  1.102
      libswscale      2.  5.101 /  2.  5.101
      libswresample   0. 17.104 /  0. 17.104
      libpostproc    52.  3.100 / 52.  3.100
    Input #0, mp3, from 'adele.mp3':
      Metadata:
        title           : Chasing Pavements
        artist          : Adele
        track           : 3
        album           : 19 (Deluxe Edition)
        date            : 2008
      Duration: 00:03:30.55, start: 0.025056, bitrate: 160 kb/s
        Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 159 kb/s
        Stream #0:1: Video: mjpeg, yuvj420p(pc), 600x600 [SAR 72:72 DAR 1:1], 90k tbr, 90k tbn, 90k tbc
        Metadata:
          title           : 
          comment         : Other
    Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'Ruby on Rails 101- What is Ruby on Rails_.mp4':
      Metadata:
        major_brand     : mp42
        minor_version   : 0
        compatible_brands: isommp42
        creation_time   : 2013-12-21 07:56:20
      Duration: 00:05:42.40, start: 0.000000, bitrate: 1214 kb/s
        Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720, 1020 kb/s, 30 fps, 30 tbr, 60 tbn, 60 tbc (default)
        Metadata:
          handler_name    : VideoHandler
        Stream #1:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 191 kb/s (default)
        Metadata:
          creation_time   : 2013-12-21 07:56:23
          handler_name    : IsoMedia File Produced by Google, 5-11-2011
    Overriding aspect ratio with stream copy may produce invalid files
    Output #0, mp4, to 'test.mp4':
      Metadata:
        title           : Chasing Pavements
        artist          : Adele
        track           : 3
        album           : 19 (Deluxe Edition)
        date            : 2008
        encoder         : Lavf55.29.101
        Stream #0:0(und): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=2-31, 1020 kb/s, 30 fps, 15360 tbn, 30 tbc (default)
        Metadata:
          handler_name    : VideoHandler
        Stream #0:1: Audio: mp3 (i[0][0][0] / 0x0069), 44100 Hz, stereo, 159 kb/s
    Stream mapping:
      Stream #1:0 -> #0:0 (copy)
      Stream #0:0 -> #0:1 (copy)
    Press [q] to stop, [?] for help
    frame=10272 fps=2793 q=-1.0 Lsize=   46908kB time=00:05:42.40 bitrate=1122.3kbits/s    
    video:42642kB audio:4102kB subtitle:0 data:0 global headers:0kB muxing overhead 0.351255
    
  • Answers
  • LordNeckbeard

    You can use the amerge and pan audio filters:

    ffmpeg -i audio.mp3 -i video.mp4 -filter_complex \
    "[0:a][1:a]amerge,pan=stereo:c0<c0+c2:c1<c1+c3[out]" \
    -map 1:v -map "[out]" -c:v copy -c:a libfdk_aac -shortest output.mp4
    

    This will take the audio stream from the first input (audio.mp3) and the audio stream from the second input (video.mp4). amerge will combine them into a 4 channel stream, then pan will combine the 4 channels into a stereo stream. The output link label is called [out] (you can use almost any arbitrary name).

    Then -map 1:v selects the video from the second input as a video source for the output file, and -map "[out]" selects the audio from the filtergraph as an audio source for the output file.

    The video is stream copied with -c:v copy, so it is not re-encoded. Filters require re-encoding, so -c:a libfdk_aac is used in this example to re-encode to AAC audio.

    The -shortest option will end the output file whenever the shortest input ends which is useful if one input is shorter than the other.


  • Related Question

    Add audio to video using ffmpeg
  • Arlen Beiler

    I'm trying to add sound to a video using ffmpeg, but for some reason, when I play the resulting file, it doesn't have any sound. I also tried doing it with an aac audio file, as well as leaving out -acodec copy and giving it a wav file.

    Here is the complete output:

    C:\Users\SM-Audio\Desktop>ffmpeg -i PrintingCDs.mp4 -i AudioPrintCDs.mp3 -acodec
     copy -vcodec copy PrintCDs1.mp4
    ffmpeg version N-48886-g5ce023b Copyright (c) 2000-2013 the FFmpeg developers
      built on Jan 14 2013 19:16:33 with gcc 4.7.2 (GCC)
      configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av
    isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
    le-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libg
    sm --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --e
    nable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --e
    nable-libspeex --enable-libtheora --enable-libtwolame --enable-libvo-aacenc --en
    able-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable
    -libxavs --enable-libxvid --enable-zlib --enable-filter=frei0r
      libavutil      52. 14.100 / 52. 14.100
      libavcodec     54. 89.100 / 54. 89.100
      libavformat    54. 59.107 / 54. 59.107
      libavdevice    54.  3.102 / 54.  3.102
      libavfilter     3. 32.100 /  3. 32.100
      libswscale      2.  1.103 /  2.  1.103
      libswresample   0. 17.102 /  0. 17.102
      libpostproc    52.  2.100 / 52.  2.100
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'PrintingCDs.mp4':
      Metadata:
        major_brand     : mp42
        minor_version   : 0
        compatible_brands: isommp42
        creation_time   : 2013-05-01 13:35:52
      Duration: 00:02:35.57, start: 0.000000, bitrate: 585 kb/s
        Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1024x
    768 [SAR 1:1 DAR 4:3], 457 kb/s, 10 fps, 10 tbr, 30k tbn, 30 tbc
        Metadata:
          creation_time   : 2013-05-01 13:35:53
          handler_name    : Mainconcept MP4 Video Media Handler
        Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 22050 Hz, mono, fltp, 126
    kb/s
        Metadata:
          creation_time   : 2013-05-01 13:35:53
          handler_name    : Mainconcept MP4 Sound Media Handler
    [mp3 @ 003dd6c0] max_analyze_duration 5000000 reached at 5015510 microseconds
    Input #1, mp3, from 'AudioPrintCDs.mp3':
      Metadata:
        encoder         : Lavf54.59.107
      Duration: 00:02:36.21, start: 0.000000, bitrate: 64 kb/s
        Stream #1:0: Audio: mp3, 44100 Hz, mono, s16p, 64 kb/s
    File 'PrintCDs1.mp4' already exists. Overwrite ? [y/N] y
    Output #0, mp4, to 'PrintCDs1.mp4':
      Metadata:
        major_brand     : mp42
        minor_version   : 0
        compatible_brands: isommp42
        encoder         : Lavf54.59.107
        Stream #0:0(eng): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1024x768 [S
    AR 1:1 DAR 4:3], q=2-31, 457 kb/s, 10 fps, 30k tbn, 30k tbc
        Metadata:
          creation_time   : 2013-05-01 13:35:53
          handler_name    : Mainconcept MP4 Video Media Handler
        Stream #0:1(eng): Audio: aac ([64][0][0][0] / 0x0040), 22050 Hz, mono, 126 k
    b/s
        Metadata:
          creation_time   : 2013-05-01 13:35:53
          handler_name    : Mainconcept MP4 Sound Media Handler
    Stream mapping:
      Stream #0:0 -> #0:0 (copy)
      Stream #0:1 -> #0:1 (copy)
    Press [q] to stop, [?] for help
    frame= 1555 fps=0.0 q=-1.0 Lsize=   11133kB time=00:02:35.57 bitrate= 586.2kbits
    /s
    video:8674kB audio:2408kB subtitle:0 global headers:0kB muxing overhead 0.459569
    %
    

  • Related Answers
  • slhck

    By default, FFmpeg will only take one audio and one video stream. In your case that's taken from the first file only.

    You need to map the streams correctly:

    ffmpeg -i input.mp4 -i input.mp3 -c copy -map 0:0 -map 1:0 output.mp4
    

    Here, 0:0 is the first stream of the first file and 1:0 is the first (and only) stream of the second file.