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.mp4

Configuring the Number of Loops

The value you pass to -stream_loop determines the looping 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-url

Important Considerations