mix audio with h264 mp4 video with ffmpeg

13
2013-09
  • Questioner

    I have 2 files :

    Input #0, wav, from '105426_1.wav':
      Duration: 00:00:09.98, bitrate: 1312 kb/s
        Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 41000 Hz, stereo, s16, 1312 kb/s
    

    and:

    Duration: 00:00:41.29, start: 0.000000, bitrate: 1313 kb/s
        Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 640x360 [SAR 1:1 DAR 16:9], 1211 kb/s, 24.42 fps, 25 tbr, 90k tbn, 48 tbc
        Metadata:
          handler_name    : VideoHandler
        Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 99 kb/s
        Metadata:
          handler_name    : SoundHandler
    

    I want to insert first audio file into video in special place (for example in 10 secunde of video) and mix it with audio stream of video file.

    I try to

     /usr/local/bin/ffmpeg  -i 105426_1.wav -i 105426.mp4 -map 0:0 -map 1:1 -map 1:0  video_finale.mp4
    

    but result is :

      Duration: 00:00:41.31, start: 0.046440, bitrate: 755 kb/s
        Stream #0:0(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s
        Metadata:
          handler_name    : SoundHandler
        Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s
        Metadata:
          handler_name    : SoundHandler
        Stream #0:2(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x360 [SAR 1:1 DAR 16:9], 588 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc
        Metadata:
          handler_name    : VideoHandler
    

    I need only one audio stream and first stream play not from beginig but from 10 sec

  • Answers
  • Scott

    I think that would be strange if ffmpeg was able to do such a thing. You should demux audio stream and mix your audio into it (e.g., using audacity) then mux.

    The second way, I guess, is to shift the second audio stream using the option -itsoffset, then try to play both streams simultaneously using media player with appropriate splitter (in this case I suggest to try out MPC-HC + ffdshow + Haali Media Splitter).


  • Related Question

    ffmpeg - h264 to xvid creates large file
  • fatnic

    I'm trying to use ffmpeg to convert a h264/aac video file to an xvid/mp3 file so I can play it in my ultra-cheap media player.

    At the moment the converted video file is TWICE the size of the original mp4. Is there any way to get a smaller file size without loosing too much quality? Even a drop to -qmin 1 is pretty awful!

    The command i'm using is

    ffmpeg -i input.mp4 -vcodec libxvid -sameq -acodec libmp3lame -ab 128k -ac 2 output.avi
    

    And the ffmpeg output is

      Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.mp4'
      Metadata:
        major_brand     : isom
        minor_version   : 1
        compatible_brands: isomavc1
      Duration: 01:34:27.69, start: 0.000000, bitrate: 1520 kb/s
        Stream #0.0(und): Video: h264, yuv420p, 720x304 [PAR 1:1 DAR 45:19], 1387 kb/s, 25 fps, 25 tbr, 25k tbn, 50 tbc
        Stream #0.1(und): Audio: aac, 48000 Hz, stereo, s16, 128 kb/s
    Output #0, avi, to 'output.avi':
      Metadata:
        ISFT            : Lavf52.64.2
        Stream #0.0(und): Video: mpeg4, yuv420p, 720x304 [PAR 1:1 DAR 45:19], q=2-31, 200 kb/s, 25 tbn, 25 tbc
        Stream #0.1(und): Audio: libmp3lame, 48000 Hz, stereo, s16, 128 kb/s
    Stream mapping:
      Stream #0.0 -> #0.0
      Stream #0.1 -> #0.1
    

  • Related Answers
  • CarlF

    Depending on what your media player can handle, you can reduce the frame size, e.g. if it starts as 1280x720, decrease to 640x360. That would let you reduce the video bitrate by 3/4 without changing the quality, and smaller video players only display 640 pixels wide anyway.

  • geocoin

    As has been said before in comments, h.264 is a better (as in compresses better) codec than xvid, so with all other things being equal (quality, framesize, framerate etc.) xvid will always produce larger files. the upside to xvid is that it's less CPU intensive than h.264 which is why cheap media players can use it.

    Case in point, XBMC on the original xbox (which had a CPU of PIII architecture running at 733 MHz) could be coerced into playing 720p HD content, but only if the codec was lightweight enough and with very specific flags. this was typically xvid as h.264 even in SD resolutions was usually too much for the poor old PIII. at SD resolutions a typical acceptable Q 45 min tv show weighs in around 350 Mb, but in h.264 would be around half that.

    for your best bet, re-code the videos to the same res as the device (but keeping the quality), that way you're not wasting any bandwidth on image that's only going to be downscaled at playback time anyway. - halving the resolution in any direction (say 1280x720 -> 640x360 as mentioned above) will reduce the required bandwidth (and resulting filesize) to 1/4.