How to Use FFmpeg Loop Filter to Repeat Frames
This article explains how to use the FFmpeg loop filter
to repeat a specific range of frames within a video. You will learn the
exact syntax of the filter, the meaning of its key parameters—such as
loop count, frame size, and start index—and see a practical command-line
example to implement this in your video processing workflow.
To repeat a specific frame range in FFmpeg, you apply the
loop video filter (-vf). This filter requires
you to define how many times the sequence should repeat, how many frames
are in the sequence, and the starting frame number of the loop.
The Loop Filter Parameters
The basic syntax for the loop filter is:
loop=loop=<count>:size=<number_of_frames>:start=<start_frame>
loop: The number of times the range is repeated. Set this to a positive integer (e.g.,3to repeat the sequence 3 times) or-1for an infinite loop.size: The total number of frames in the range you want to loop.start: The 0-based index of the first frame where the loop begins.
Practical Command Example
Suppose you have a video playing at 30 frames per second (fps). You want to repeat a 2-second segment (which equals 60 frames) that starts at the 5-second mark (frame index 150). You want this 60-frame segment to repeat 4 times.
You can achieve this with the following command:
ffmpeg -i input.mp4 -vf "loop=loop=4:size=60:start=150" -c:a copy output.mp4Key Considerations
- Calculating Frames: Because the
startandsizeparameters require frame numbers rather than timestamps, you must calculate them based on your video’s frame rate. Multiply your target time in seconds by the video’s FPS (e.g., 5 seconds * 30 fps = frame 150). - Audio Sync: The
loopvideo filter only repeats video frames. If your source file has audio, the audio will continue playing linearly while the video loops, which will desynchronize the track. To loop audio in sync with the video, you must apply thealoopfilter to the audio stream or remove the audio entirely using the-anflag.