adding repeated background audio with ffmpeg

20,815

Solution 1

ffmpeg has the promising -loop_input flag, but it doesn't support audio inputs yet.

I'd recommend sox and the -shortest option for ffmpeg as a solution.

sox short_audio.mp3 looped_audio.mp3 repeat 1000 # adjust count as necessary
ffmpeg -i input_video.mp4 -i looped_audio.mp3 -shortest output_video.mp4

The sox command will loop the input, and the ffmpeg command will use it for the audio, but stop when it runs out of video to process.

Solution 2

I ran into the same problem and managed to do it by using ffmpeg and concat[enate] filter. Here is an example of how to loop it three times:

ffmpeg -i audio.wav -filter_complex "[0:a]afifo[a0];[0:a]afifo[a1];[0:a]afifo[a2];[a0][a1][a2]concat=n=3:v=0:a=1[a]" -map "[a]" out.wav

Edited (23/12/2020)

In addition to the above, another super easy 2 methods :

1st method : with audio output SIZE defined.

meaning : it will repeat / concatenate "MyAudio.mp3" till it reaches size of 10M and stop ( you will need to calculate end size yourself )

ffmpeg -stream_loop -1 -i "MyAudio.mp3" -fs 10M -c copy "MyRepeatingAudio.mp4"

2nd method : with NO output SIZE defined ( you will need to stop process, CTRL+C once it reaches required size

ffmpeg -stream_loop -1 -i "MyAudio.mp3" -c copy "MyRepeatingAudio.mp4"

Please note that above methods are also for REPEATING VIDEOS

3rd-party edit (15/09/2021)

This command adds repeating audio to a video in a single step:

ffmpeg -i input.mp4 -stream_loop -1 -i audio.mp4 -shortest \
    -map 0:v:0 -map 1:a:0 -c:v copy output.mp4
Share:
20,815

Related videos on Youtube

象嘉道
Author by

象嘉道

枫林术士 MAPLE HERMIT ☵☯☲

Updated on September 15, 2021

Comments

  • 象嘉道
    象嘉道 almost 3 years

    With ffmpeg, I see how to add music file as background for a video, but the problem is how to make the audio loop/repeat. Is there a way out?

    • rupello
      rupello about 13 years
      I don't think so. I think you'll have to generate your looping audio track offline before combining it with the video
    • jamadagni
      jamadagni about 6 years
      Option -stream_loop for ffmpeg exists since dbb03b8e, as per trac.ffmpeg.org/ticket/2584
  • Ivan Kochurkin
    Ivan Kochurkin over 11 years
    -i parameter is deprecated in sox. Is is properly to use -e ima-adpcm instead.
  • Gyan
    Gyan about 8 years
    This is a direct cmd: ffmpeg -lavfi "amovie=audio.wav:loop=3" out.wav
  • nanestev
    nanestev about 8 years
    @Mulvya, thanks. Unfortunately, it doesn't work for me. I'm using the latest static linked build of ffmpeg for Windows and when I try the command you shared it gives me: [auto-inserted resampler 0 @ 00000000025fc0a0] Channel layout change is not supported Error while filtering: Not yet implemented in FFmpeg, patches welcome
  • Gyan
    Gyan about 8 years
    Share the full console output.
  • nanestev
    nanestev about 8 years
    Here it is - take.ms/QAWmk
  • Gyan
    Gyan about 8 years
    What is the channel layout of track1?
  • nanestev
    nanestev about 8 years
    ffmpeg -i "track1.wav" /b /o:gn says 2 channels, so it's stereo.
  • M3D
    M3D almost 7 years
    See here, you can use asplit: ffmpeg.org/ffmpeg-filters.html#Examples-120 [in] asplit=3 [out0][out1][out2]
  • ND1010_
    ND1010_ over 6 years
    @blahdiblah can you tell me what is short_audio.mp3 and what is looped_audio.mp3 from my thinking short_audio.mp3 is input path from local and looped_audio.mp3 is out put file to stored looped sound then after later looped_audio.mp3 will be use to merge video am i right ?
  • ajay dhadhal
    ajay dhadhal over 5 years
    how to Looping music over video?
  • Roel Van de Paar
    Roel Van de Paar about 5 years
    @nanestev excellent command. This was the only thing I could get to work in a complex ffmpeg command (and I tried a lot) to merge various video's with repeated audio. For some reason the loop (and aloop) commands were not working for me. For anyone reading this, note that you can add the filter_complex string at the end of an existing video-merging filter_complex string simply separating it with a semicolon. i.e. -filter_complex "....your_existing_filter_complex...;[0:a]afifo[a0]; ... etc". Thanks again.
  • Roel Van de Paar
    Roel Van de Paar about 5 years
    Old reply now no longer relevant. Ref below.
  • Roel Van de Paar
    Roel Van de Paar about 5 years
    And (thank you!) this made me realize there is another better way still, which also seems to avoid some "[out_0_1 @ 0x55f6ec424340] 10000 buffers queued in out_0_1, something may be wrong." ffmpeg errors with the above command, and you can do it like this; specify multiple audio inputs, then merge them, i.e. ffmpeg -i audio.mp3 -i audio.mp3 -i video1.mp4 -i video2.mp4 -filter_complex "[2:v][3:v]concat=n=2:v=1:a=0[v];[0:a][1:a]concat=n=2:v=0:a=‌​1[a]" -shortest -map "[v]" -map '[a]' "out.mp4" - just make sure enough audio is there to cover the video! Enjoy!
  • Roel Van de Paar
    Roel Van de Paar about 5 years
    Hrm. -shortest does not work in this case. See long standing bug: trac.ffmpeg.org/ticket/3789
  • mrVerma
    mrVerma over 2 years
    Please note that for large files, this process will be very slow. You can use -c:v copy to stream copy (mux) the video. No re-encoding of the video occurs. Quality is preserved and the process is fast. [For more details, see @llogan 's answer on stackoverflow.com/questions/11779490/…