6

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.

2
11

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
8
  • 1
    The 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!
    – Tomcatus
    Oct 4 '14 at 19:14
  • @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…
    – slhck
    Oct 4 '14 at 19:16
  • 1
    @slhck What is the feature request URL and was it implemented? Thanks May 31 '19 at 1:47
  • 1
    @RoelVandePaar This was the feature request, and -stream_loop was implemented.
    – slhck
    May 31 '19 at 7:11
  • 1
    @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? May 31 '19 at 8:18
7

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

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.