How to Use FFmpeg stream_loop to Loop Video
This article provides a quick and practical guide on how to use the
-stream_loop option in FFmpeg to repeat an input video. You
will learn the correct command syntax, how to set a specific number of
loops, how to loop a video infinitely, and how to combine looping with
duration limits for your video processing workflows.
The Basic Syntax
The -stream_loop option is an input
option. This means it must be placed before
the input file (-i) in your FFmpeg command. If you place it
after the input file, FFmpeg will ignore it or throw an error.
The basic syntax is:
ffmpeg -stream_loop <number_of_loops> -i input.mp4 [output_options] output.mp4Loop Count Values:
- Positive Integer (\(N\)): Loops the input \(N\) times. Note that the total plays will be \(N + 1\) (the original play plus the \(N\) loops).
0: No looping (default behavior).-1: Loops the input infinitely.
Practical Examples
1. Loop a Video a Specific Number of Times
To loop an input video 3 times (resulting in the video playing 4 times in total) without re-encoding the stream, use the following command:
ffmpeg -stream_loop 3 -i input.mp4 -c copy output.mp4Using -c copy stream-copies the packets without
re-encoding, making the process extremely fast.
2. Loop a Video Infinitely
To loop a video endlessly, use -1. Because an infinite
loop will run forever and create an infinitely large file, this is
typically used for live streaming.
If you want to save an infinite loop to a local file, you must
specify a target duration using the -t option (in seconds)
so the command knows when to stop:
ffmpeg -stream_loop -1 -i input.mp4 -t 60 -c copy output.mp4This command loops input.mp4 infinitely but stops
the output exactly at the 60-second mark.
Key Considerations
Placement Matters: Always place
-stream_loopbefore-i.- Correct:
ffmpeg -stream_loop 2 -i input.mp4... - Incorrect:
ffmpeg -i input.mp4 -stream_loop 2...
- Correct:
Audio Sync: When using
-c copy, some containers (like MP4) might experience minor audio sync issues at the loop transition points. If you notice audio glitches, force a re-encode by removing-c copy:ffmpeg -stream_loop 3 -i input.mp4 output.mp4