How to Loop Input Files in FFmpeg with -stream_loop
This article provides a straightforward guide on how to configure the
number of loops for input files in FFmpeg using the
-stream_loop option. You will learn the correct syntax,
where to place the option in your command line, and how to set specific
loop counts or create infinite loops for your audio and video
streams.
The Syntax of -stream_loop
The -stream_loop option defines how many times an input
stream should be repeated. It is an input option,
meaning it must be placed before the input file
(-i) that you want to loop in your FFmpeg command.
The basic syntax is:
ffmpeg -stream_loop <number_of_loops> -i input.mp4 [output_options] output.mp4Configuring the Number of Loops
The value you pass to -stream_loop determines the
looping behavior:
- Positive Integer (
N): Loops the inputNtimes. The total playback will be the original file plusNrepetitions (total plays =N + 1). - Infinite Loop (
-1): Loops the input indefinitely. - No Loop (
0): Plays the input once with no repetitions (this is the default behavior).
Examples
1. Loop an Input Video 3 Times
To repeat a video 3 times (resulting in 4 total plays), place
-stream_loop 3 before your input file:
ffmpeg -stream_loop 3 -i input.mp4 -c copy output.mp4(Note: Using -c copy is recommended when looping to
avoid re-encoding and save processing time).
2. Loop an Input Infinitely
To loop a video or audio file infinitely, set the loop value to
-1. This is commonly used when streaming a local file to a
live streaming destination:
ffmpeg -stream_loop -1 -i input.mp4 -f flv rtmp://your-streaming-urlImportant Considerations
- Placement is Critical: If you place
-stream_loopafter the input file (e.g.,ffmpeg -i input.mp4 -stream_loop 2 output.mp4), FFmpeg will ignore the option or throw an error. It must always precede-i. - Audio and Video Sync: The
-stream_loopoption loops the entire container. If your input file has audio and video streams of different durations, they will be looped together based on the container’s duration.