Add audio to video using ffmpeg

23
2013-08
  • 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
    %
    
  • 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.


  • Related Question

    Would like to change audio codec, but keep video settings with ffmpeg
  • Craig Tataryn

    I have a video for which I'd like to convert the audio codec to AAC 320 kbps / 44.100 kHz. What would I use for ffmpeg switches such that all the video settings and codec remain the same, but only the audio codec and settings change?

    Here's my video:

    $ ffmpeg -i Winnipeg.rb\ Scala-Talk.mov FFmpeg version SVN-r25375, Copyright (c) 2000-2010 the FFmpeg developers
      built on Oct  6 2010 13:02:41 with gcc 4.2.1 (Apple Inc. build 5664)
      configuration: --enable-libmp3lame --enable-shared --disable-mmx --arch=x86_64
      libavutil     50.32. 2 / 50.32. 2
      libavcore      0. 9. 1 /  0. 9. 1
      libavcodec    52.92. 0 / 52.92. 0
      libavformat   52.80. 0 / 52.80. 0
      libavdevice   52. 2. 2 / 52. 2. 2
      libavfilter    1.48. 0 /  1.48. 0
      libswscale     0.12. 0 /  0.12. 0
    
    Seems stream 0 codec frame rate differs from container frame rate: 2000.00 (2000/1) -> 10.00 (10/1)
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'Winnipeg.rb Scala-Talk.mov':
      Metadata:
        major_brand     : qt  
        minor_version   : 537199360
        compatible_brands: qt  
      Duration: 01:10:53.00, start: 0.000000, bitrate: 283 kb/s
        Stream #0.0(eng): Video: h264, yuv420p, 800x598, 94 kb/s, 10 fps, 10 tbr, 1k tbn, 2k tbc
        Stream #0.1(eng): Audio: adpcm_ima_qt, 22050 Hz, 1 channels, s16
        Stream #0.2(eng): Audio: adpcm_ima_qt, 22050 Hz, 1 channels, s16
    At least one output file must be specified
    

    Many thanks in advance! One with with ffmpeg I've never been able to grok is how to just "tweak" files without having to regurgitate every little setting for things you don't want changes.


  • Related Answers
  • harrymc

    Hard to judge what you are doing, since the ffmpeg command was a bit garbled in your post.

    To replace an audio track:

    ffmpeg -i test.avi -i normalized.mp3 -map 0:0 -map 1:0 -vcodec copy -acodec copy new_test.avi
    

    More information about the above parameters is found in: FFmpeg Documentation.

  • Benny

    Why don't you use Handbrake which lets keep the video and change the audio and it is a GUI for FFmpeg and it works like a charm.