How to Configure FFmpeg Loop Filter Count
Configuring the loop count in the FFmpeg loop filter
allows you to repeat a specific segment of a video or audio stream a set
number of times. This article provides a quick and clear guide on how to
define the loop count, specify the size of the loop, and set the
starting frame using FFmpeg command-line arguments.
The Loop Filter Syntax
To configure the loop count, you must use the loop video
filter (-vf "loop=...") or the aloop audio
filter (-af "aloop=..."). The filter relies on three
primary parameters:
loop: The number of loops. Set this to-1for an infinite loop,0for no loop, or any positive integer (e.g.,5) to repeat the segment that exact number of times.size: The number of frames (for video) or samples (for audio) to be kept in the loop buffer.start: The 0-based index of the first frame or sample where the loop begins.
Command Examples
1. Loop a Video Segment 5 Times
To loop a sequence of 100 frames, starting from the very first frame (index 0), a total of 5 times:
ffmpeg -i input.mp4 -vf "loop=loop=5:size=100:start=0" output.mp42. Infinite Video Looping
To loop a 150-frame segment starting at frame 50 infinitely, set the
loop parameter to -1.
ffmpeg -i input.mp4 -vf "loop=loop=-1:size=150:start=50" output.mp4Note: Infinite looping is typically used when outputting to formats that support infinite playback, like animated GIFs, or when streaming.
3. Loop an Audio Segment
For audio streams, use the aloop filter. Because audio
uses samples instead of frames, the size and
start values must be calculated in audio samples. For
example, to loop 1 second of a 44.1kHz audio file 3 times from the
beginning:
ffmpeg -i input.wav -af "aloop=loop=3:size=44100:start=0" output.wav