34

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?

4 Answers 4

43

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.

8
  • 1
    Can you describe how these are different? Commented Mar 4, 2020 at 15:39
  • 4
    How to do it when the video has no audio?
    – minseong
    Commented Jun 14, 2020 at 15:14
  • This is working perfectly. Was not working previously. Update the FFMPEG if it is not working for you. Commented Dec 2, 2020 at 10:23
  • @minseong, -map 0:v:0 -map 1:a:0 already do its job. It will take only video from the first -i and only audio from the second -i no matter what your second input is video or audio file.
    – ninhjs.dev
    Commented Mar 11, 2021 at 12:39
  • 1
    @Wolfpack'08 He didn't explained but as you can see there's a difference between putting -stream_loop -1 before or after the determined input i.e. -i file.mp3 or -i file.mp4 Commented Feb 7, 2023 at 20:48
1

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.

2
  • 1
    This is barely half an answer, it's just a link that only addresses half the question.
    – Zim
    Commented Jun 20, 2019 at 17:28
  • @Zim it was clearly stated in the answer that it addresses only the second bullet point. Commented Aug 4, 2019 at 3:04
1

For the first bullet point I don't see a direct command. What I'm suggesting is to use ffprobe to calculate the duration of both files and calculate the number of video loops that need to play for the duration of audio.

Following will result the duration of the inputs.

ffprobe -i input_media -show_entries format=duration -v quiet -of csv="p=0"

Then you can loop the video manually until you satisfy the calculated value of video instances.

ffmpeg -i input_audio -f concat -i <(for i in {1..n}; do printf "file '%s'\n" input_video; done) -c:v copy -c:a copy -shortest output_video

1..n stands for the number of times the video need to be looped. Instead of -shortest you can use -t as you have already calculated the duration of the audio.

Following is the associated shell script.

#!/bin/bash
audio_duration=$(ffprobe -i ${1} -show_entries format=duration -v quiet -of csv="p=0")
video_duration=$(ffprobe -i ${2} -show_entries format=duration -v quiet -of csv="p=0")
n_loops=$(echo "(${audio_duration} / ${video_duration}) + 1"|bc)
ffmpeg -i ${1} -f concat -i <(for i in {1..${n_loops}}; do printf "file '%s'\n" ${2}; done) -c:v copy -c:a copy -shortest ${3}

For windows you can convert this shell script to a batch script.

More information:

Hope this helps!

2
  • Thanks! Unfortunately I don't think I can use this shell scripting on Windows. Would you have a Windows version?
    – Basj
    Commented Jul 14, 2015 at 17:24
  • I'm sorry. I have no pc running on windows. Still you can followup the updated link in the answer.
    – Chamath
    Commented Jul 15, 2015 at 9:26
-2

for the first one, try ffmpeg -i yourmovie.mp4 -loop 10 to loop the input 10 times

2
  • 5
    -loop_input is deprecated, use -loop 1 Note, both loop options only work with -f image2
    – puchu
    Commented Mar 5, 2012 at 18:42
  • 2
    can't get -loop 10 nor -loop 1 to work. I'm trying to loop a mp4 video (3 minutes) to audio that is 2 hours long.
    – Sun
    Commented Dec 31, 2014 at 22:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.