Merge video and audio with ffmpeg. Loop the video while audio is not over

audioffmpegvideo

I'm trying to merge an audio file with a video file.

I have two options:

  • Have a very small video file (e.g., 10 seconds) that loop while the audio file is not over.

  • Have a very long video file (longer than any of my audio file) on which I can attach the audio file. I would like to cut the video when the audio is finished.

I'm using the latter with the -t <duration in second> option of ffmpeg. It means I have to get the duration of the audio file to feed it into ffmpeg. Is it possible to avoid this step?

Any pointer for the first solution?

Best Answer

If you want to merge new Audio with video repeat video until the audio finishes, you can try this:

ffmpeg  -stream_loop -1 -i input.mp4 -i input.mp3 -shortest -map 0:v:0 -map 1:a:0 -y out.mp4

Using -stream_loop -1 means infinite loop input.mp4, -shortest means finish encoding when the shortest input stream ends. Here the shortest input stream will be the input.mp3.

And if you want to merge new Audio with video repeat audio until the video finishes, you can try this:

ffmpeg  -i input.mp4 -stream_loop -1 -i input.mp3 -shortest -map 0:v:0 -map 1:a:0 -y out.mp4

use -y option will overwrite output files without asking.

Related Topic