Search code examples
python-3.xffmpeg

ffmpeg - add music to an audiobook, and loop the music


I found these example, and they all work. But I cannot overlay 2 audios and loop the shortest audio until the longest audio is finished.

Audio to match Video Length

ffmpeg -i VIDEO1.mp4 -stream_loop -1 -i bgmusic.mp3 -shortest -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 output.mp4

Loop Audio

ffmpeg -i audio.mp3 -filter_complex "aloop=loop=-1:size=10" output_loop.mp3

Overlay 2 Audios

ffmpeg -y -i bgmusic.mp3 -i audio.mp3 -filter_complex "[0:0]volume=1.0[a];[1:0]volume=1.0[b];[a][b]amix=inputs=2:duration=longest" -c:a libmp3lame output.mp3

Here is my attempt.

ffmpeg -y -i audio.mp3 -stream_loop -1 -i bgmusic.mp3 -shortest -filter_complex "[0:0]volume=1.0[a];[1:0]volume=1.0[b];[a][b]amix=inputs=2:duration=longest" -c:a libmp3lame output.mp3

But this example is looping bgmusic.mp3 forever ... and audio.mp3 is stopping after the song finishes

how can I get to audios to play togeather, but the shortest audio loops until the longest audio is finished?


Solution

  • Thank you, I have somehow stumbled onto the solution!

    ffmpeg -y -i audio.mp3 -stream_loop -1 -i bgmusic.mp3 -filter_complex "[0:0]volume=1.0[a];[1:0]volume=1.0[b];[a][b]amix=inputs=2:duration=shortest" -c:a libmp3lame output.mp3
    

    This seems to be working, its able to play the full duration of audio.mp3 ... and loop bgmusic.mp3 ... until audio.mp3 is done.