12

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.

Tomcatus
  • 370
  • 1
  • 3
  • 12

4 Answers4

12

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
Eugen Rieck
  • 20,637
9

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

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
Alex
  • 11
0

This loops the audio until the video ends:

ffmpeg -i video.mp4 -stream_loop -1 -i audio.mp4 -shortest -map 0:v -map 1:a -c copy output.mp4

-stream_loop -1 makes the audio input loop "-1" (infinite) times.
-shortest makes it "Finish encoding when the shortest output stream ends."

Puddle
  • 101