1

I want to mix audio on a video. The video already has audio stream in it. I'm adding another one.

The new audio I'm adding is shorter than the video. So, I want to loop it as long as the video lasts.

So far, I'm doing this:

ffmpeg -y -i test.mp4 -stream_loop -1 -i bg_sound.mp3 -filter_complex "[0:a][1:a]amix=inputs=2:duration=longest[out]" -map 0:v -map [out] output.mp4

This adds the audio on the video, but does not loop it. I've also tried stream_loop with other integer options with no luck. The audio I'm adding ends and the rest of the video continues.

I need to loop my audio.

Here is the ffprobe result of my video:

   ffprobe version N-76452-ga97f1e7 Copyright (c) 2007-2015 the FFmpeg developers
   built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04)
   configuration: --prefix=/home/alfanso/ffmpeg_build --pkg-config-flags=--static --extra-cflags=-I/home/alfanso/ffmpeg_build/include --extra-ldflags=-L/home/alfanso/ffmpeg_build/lib --bindir=/home/alfanso/bin --enable-gpl --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree
   libavutil      55.  5.100 / 55.  5.100
   libavcodec     57. 14.100 / 57. 14.100
   libavformat    57. 14.100 / 57. 14.100
   libavdevice    57.  0.100 / 57.  0.100
   libavfilter     6. 14.101 /  6. 14.101
   libswscale      4.  0.100 /  4.  0.100
   libswresample   2.  0.100 /  2.  0.100
   libpostproc    54.  0.100 / 54.  0.100
   Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'localtest.mp4':
   Metadata:
       major_brand     : isom
       minor_version   : 512
       compatible_brands: isomiso2avc1mp41
       encoder         : Lavf56.40.101
       Duration: 00:06:53.97, start: 0.033333, bitrate: 179 kb/s
       Stream #0:0(und): Video: h264 (High 4:4:4 Predictive) (avc1 / 0x31637661), yuv444p, 1280x720, 45 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
   Metadata:
      handler_name    : VideoHandler
      Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, mono, fltp, 128 kb/s (default)
   Metadata:
      handler_name    : SoundHandler

I've also tried using different video and audio sources with the same result.

7
  • Try assigning something 5 or 6 to stream loop and change to shortest in filter. – Gyan Dec 18 '15 at 12:23
  • I had tried with stream_loop 5. Just tried that along with shortest in amix. That caused the original audio (already in video) to stop along with the video being added. I hope this makes sense :P – Dhavan Vaidya Dec 18 '15 at 12:32
  • Ok, I'll check when I have more time. – Gyan Dec 18 '15 at 12:33
  • Just checking in :) Should I report this as feature request/bug? – Dhavan Vaidya Dec 21 '15 at 3:51
  • Sorry. Busy. Give me a day or so. – Gyan Dec 21 '15 at 4:43
2

It works if you mix audio separately.

ffmpeg -y -i test.mp4 \
-f lavfi -i amovie=bg_sound.mp3:loop=55555 \
-filter_complex "[0:a][1:a]amerge, \
                 pan=stereo|c0=0.5*c0+0.5*c2|c1=0.5*c1+0.5*c3" \
output.flac
4
  • Just to confirm, will the pan work if the two audio inputs are mono? – Gyan Feb 9 '16 at 12:35
  • for mono you need to change pan parameters, or not use pan at all, just amerge. – user12174 Feb 9 '16 at 15:06
  • So, maybe the amix is the better option for the OP. – Gyan Feb 9 '16 at 15:08
  • amix can't be worked around to accept looped audio, it simply stops. – user12174 Feb 9 '16 at 15:11
1

I have tried the following command to generate output file with repeating audio track and it is working fine

-stream_loop 2 -i input.mp3 -c copy -y output.mp3

it will create output.mp3 output file with repeating input track 3 times

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.