brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ffmpeg -stream_loop repeats a clip without re-encoding

Ffmpeg
~/tips/tip-ffmpeg-stream-loop-repeat

ffmpeg -stream_loop 2 -i in.mp4 -c copy out.mp4

Repeats an input a set number of times, which is handy for loops and background videos.

A short clip that has to fill a longer slot usually means a pile of copy-paste in an editor. FFmpeg can repeat the input itself with one option.

ffmpeg -stream_loop 2 -i in.mp4 -c copy out.mp4

-stream_loop sets the number of times the input is looped, and it is an input option, so it goes before the -i it belongs to. The number counts extra repeats: 2 plays the clip three times in total. A value of -1 loops forever, which only makes sense together with a limit such as -t 60 on the output.

With -c copy nothing is re-encoded, so it is quick and loses no quality.

The caveat is the seam. Without re-encoding, the join between two repeats can show a small jump in sound or a timestamp hiccup, depending on the clip. If a player stutters at the loop point, drop -c copy and let FFmpeg re-encode.

For an endless loop with a fixed length, combine it: ffmpeg -stream_loop -1 -i in.mp4 -t 60 -c copy out.mp4.

In a throwaway test with a generated 2 second clip, -stream_loop 2 -c copy gave a 6 second file. The endless variant with -t 5 came out at about 5.2 seconds, so with stream copy the cut lands on the nearest packet and the length can be slightly off. Re-encode if you need an exact duration.

Further reading: FFmpeg documentation: stream_loop

More Ffmpeg tips ffmpeg -i in.mp4 \ ffmpeg -i video.mp4 -i video.en.srt -map 0 -map 1 -c copy -c:s mov_text -metadata:s:s:0 language=eng video-subs.mp4 ffmpeg -i film.mkv -map 0:v -map 0:a:0 -c:v copy -c:a aac -b:a 192k -ac 2 film-stereo.mkv ffmpeg -i talk.mp4 -vn -c:a copy talk.m4a ffmpeg -n -i "$f" -c copy "${f%.mkv}.mp4" All Ffmpeg tips (38) All topics

Link