How to Loop Video Frames with FFmpeg Loop Filter

This article provides a straightforward guide on how to use the loop video filter in FFmpeg to repeat a specific sequence of frames within a video. You will learn the syntax of the filter, the key parameters required to define the loop, and see practical command-line examples to implement this effect in your projects.

The loop filter in FFmpeg allows you to repeat a specific segment of video frames. This is highly useful for creating GIFs, extending a static background, or repeating a specific action in a video clip.

The Loop Filter Syntax

The basic syntax for the loop filter is:

-vf "loop=loop=<count>:size=<frames>:start=<frame_number>"

Parameter Breakdown

  • loop: The number of times the sequence should repeat. Set this to -1 for infinite loops (useful for GIFs), or a positive integer (e.g., 5 to repeat the sequence five times). A value of 0 disables looping.
  • size: The number of frames in the sequence that you want to loop. For example, if your video runs at 30 frames per second (fps) and you want to loop a 2-second segment, the size should be 60.
  • start: The 0-based index of the first frame where the loop begins. For example, to start the loop at the 5-second mark of a 30 fps video, set the start to 150.

Practical Examples

Example 1: Loop a 100-frame sequence 3 times starting at frame 50

To repeat a sequence of 100 frames three times, starting from the 50th frame of the input video, use the following command:

ffmpeg -i input.mp4 -vf "loop=loop=3:size=100:start=50" -c:a copy output.mp4

Example 2: Create an infinite loop of the first 50 frames

If you want to loop the first 50 frames of a video indefinitely (often used when outputting to formats like GIF), set the loop count to -1 and the start frame to 0:

ffmpeg -i input.mp4 -vf "loop=loop=-1:size=50:start=0" output.gif

Important Considerations

  • Audio Sync: The loop filter only affects the video stream. The audio will continue to play normally without looping. If you need to loop the audio alongside the video, you must use the aloop audio filter in conjunction with the video filter, or loop the entire file using the -stream_loop input option.
  • Memory Usage: FFmpeg buffers the looped frames in memory. Extremely large size values (thousands of frames) can consume a significant amount of system RAM.