Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 542
fstephany Avatar asked Feb 16 '11 11:02

fstephany


People also ask

How do I combine two videos in android programmatically Ffmpeg?

ffmpeg -f concat -i mylist.txt -c copy output.mp4 This process is a way of concatenating the files and not re-encoding. Hence, it should be done speedily & conveniently. If there is no error message, you are sure to receive a final merged video named by 'output. mp4' in the MP4 folder.


2 Answers

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.

like image 113
hcf1425 Avatar answered Sep 17 '22 21:09

hcf1425


The second bullet point was easily solved by using the -shortest option of ffmpeg. See: http://www.ffmpeg.org/ffmpeg-doc.html#SEC12 for more details.

like image 27
fstephany Avatar answered Sep 20 '22 21:09

fstephany