Loop audio file to a given length
Solution 1
ffmpeg can do that for you, but you might need two steps
Optional Step 1: Find length of original file
ffmpeg -i '/path/to/original.mp3' 2>&1 | grep 'Duration :'
Now you can calculate the number of repetitions necessary. As an alternative, you can just use a "safe" number of repetitions, as too many won't hurt.
Step 2: Loop the file and cut it to needed length
create "concat.txt" with this content
file '/path/to/original.mp3'
file '/path/to/original.mp3'
...
file '/path/to/original.mp3'
file '/path/to/original.mp3'
file '/path/to/original.mp3'
It must have at least as many lines as repetitions are necessary, but, again, more won't hurt, so you can use a safe (too high) line count
And run ffmpeg (assuming you want 123.456 seconds):
ffmpeg -t 123.456 -f concat -i concat.txt -c copy -t 123.456 output.mp3
Solution 2
This can be achieved by using (appending) the below command:
-stream_loop -1 -i D:\music\mp4.mp3
This means that the D:\music\mp4.mp3 file should be looped infinitely, until the video ends.
https://ffmpeg.org/ffmpeg.html
Related videos on Youtube
Tomcatus
Updated on September 18, 2022Comments
-
Tomcatus over 1 year
I'm currently facing an issue where I need to take some mp3 file and make another mp3 file where the first one is playing for a given amount of time, looped if needed. Preferably I'm looking for a command line solution. Tried ffmpeg and sox, but couldn't find a solution with them. So now I'm looking for some options.
A further explanation: Lets say I have a file foo.mp3, I need to create bar.mp3 file that has some given length, lets say 30 seconds and that contains the foo.mp3 file, and if foo.mp3 is shorter than 30 seconds it gets looped so many times that it fills the whole 30 seconds. I hope now it's clear what I'm asking for.
-
Sun over 9 yearsCan you clarify your question. It is not clear to me what it is you're asking.
-
jamadagni almost 6 yearsOption -stream_loop for ffmpeg exists since dbb03b8e, as per trac.ffmpeg.org/ticket/2584 linked above.
-
-
Tomcatus over 9 yearsThe solution is not maybe elegant, but it does what I need. Maybe I should put a feature request for ffmpeg developers to put something like -t option with loop option, or maybe just a loop option with a parameters of seconds to loop. But still, thanks for your help Eugen! This solves my problem, cheers!
-
slhck over 9 years@Tomcatus Like I mentioned above, there's already a feature request. Looping and then using
-t
would achieve what you want if it were implemented… -
Eugen Rieck over 9 yearsI routinely use this with a "safe" number of lines - the first
-t
will make sure you don't read all that unnecessary stuff -
Roel Van de Paar almost 5 years@slhck What is the feature request URL and was it implemented? Thanks
-
slhck almost 5 years@RoelVandePaar This was the feature request, and
-stream_loop
was implemented. -
Roel Van de Paar almost 5 years@slhck thank you very much!
-
Roel Van de Paar almost 5 years@slhck so I try
ffmpeg -y -stream_loop 0 -i "audio1.mp3" -i video1.mp4 ... -map 1:v -map 0:a "out.mp4"
but the audio file does not loop (i.e. there is video with silence after the audio file plays once). Any ideas? -
slhck almost 5 years@RoelVandePaar Not from the top of my head. Please open a new question and include the full, uncut command line output!