17

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?

2
  • I don't think so. I think you'll have to generate your looping audio track offline before combining it with the video – rupello Jun 11 '11 at 0:08
  • Option -stream_loop for ffmpeg exists since dbb03b8e, as per trac.ffmpeg.org/ticket/2584 – jamadagni May 23 '18 at 13:48
24

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.

4
  • 5
    -i parameter is deprecated in sox. Is is properly to use -e ima-adpcm instead. – Ivan Kochurkin Mar 8 '13 at 20:51
  • @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 ? – ND1010_ Mar 1 '18 at 5:09
  • how to Looping music over video? – ajay dhadhal Jan 26 '19 at 13:18
  • Old reply now no longer relevant. Ref below. – Roel Van de Paar May 31 '19 at 2:14
7

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

10
  • 6
    This is a direct cmd: ffmpeg -lavfi "amovie=audio.wav:loop=3" out.wav – Gyan Jun 8 '16 at 18:13
  • @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 – nanestev Jun 9 '16 at 9:52
  • Share the full console output. – Gyan Jun 9 '16 at 10:03
  • Here it is - take.ms/QAWmk – nanestev Jun 9 '16 at 11:05
  • What is the channel layout of track1? – Gyan Jun 9 '16 at 11:18

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