10

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
  • Can you clarify your question. It is not clear to me what it is you're asking.
    – Sun
    Commented Oct 4, 2014 at 17:59
  • 1
    Option -stream_loop for ffmpeg exists since dbb03b8e, as per trac.ffmpeg.org/ticket/2584 linked above.
    – jamadagni
    Commented May 23, 2018 at 13:49

3 Answers 3

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

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

1
  • Unfortunately, it doesn't work. I tried this: ffmpeg -i input.mp4 -stream_loop -1 -i audio.wav -c:v copy -map 0:v:0 -map 1:a:0 -y new.mp4 And the file new.mp4 contains silence after the length of the audio file, so it didn't loop audio. Commented Jan 23, 2023 at 11:31
1

Single line solution:

ffmpeg -f concat -safe 0 -i <(for i in {1..8} ; do echo "file '$PWD/sample.wav'" ; done) -c copy output.wav
1
  • I get the errors "Unable to find a suitable output format for '/dev/fd/62'" and "/dev/fd/62: Invalid argument" Commented Feb 11, 2023 at 21:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .