3

E.g. if video is 1 minute, but audio is 1 hour. Make video loop throughout for 1 hour of the audio track.

I use this solution to loop image through the lenght of the audio

ffmpeg -y -loop 1 -r 6 -i image.jpg -i audio.mp3 -shortest -c:v libx264 -preset ultrafast -tune stillimage -pix_fmt yuv420p -c:a copy output.mp4

But it doesn't work for video, replacing image.jpg with video.mp4 will give error no loop found.

1
6

For video, stream_loop option should be used.

ffmpeg -y -stream_loop -1 -i video -i audio.mp3 -fflags +shortest -max_interleave_delta 50000 -c copy output.mp4

You'll have to use a very recent version of ffmpeg (> Nov '17) as stream_loop had a bug, only recently fixed.

Since video encoding is expensive in terms of time and CPU, I've used streamcopy. For most videos, this is fine. Else, you'll have to re-encode.


Edit: another bug related to stream_loop, see workaround below

ffmpeg  -stream_loop -1 -i 1.mp4 -c copy -v 0 -f nut - | ffmpeg -thread_queue_size 10K -i - -i 1.mp3 -c copy -map 0:v -map 1:a -shortest -y out.mp4
9
  • Hello. This just strips the audio to the length of video, I meant opposite. Loop video throughout to the length of the audio. For now I'm using solution at video.stackexchange.com/questions/12905/… Feb 11 '18 at 20:18
  • Use edited command.
    – Gyan
    Feb 12 '18 at 5:23
  • I set the video part to video.mp4, video plays until it's original length and stops while audio keeps going, video just stills last frame throughout. Feb 12 '18 at 22:16
  • Share full log.
    – Gyan
    Feb 13 '18 at 5:05
  • 1
    See new command at end
    – Gyan
    Feb 13 '18 at 16:04
1

Well, now the default behavior of current/latest ffmpeg via:

c:\sbin\ffmpeg.exe -i "D:\video.m4s" -i "\audio.m4s" -c:v copy -c:a copy "D:\combined.mp4"

is padding/looping the last frame of the video until audio.length === video.length.

See if this is acceptable for you.

For details behavior:

  • if (audio.length < video.length), then pad the audio with silent, so that audio.length === video.length
  • if (audio.length > video.length), then pad the video with the last frame of this video, so that audio.length === video.length

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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