Configure FFmpeg Loop Filter Start Frame and Count

This guide explains how to use the FFmpeg loop video filter to repeat a specific sequence of frames within a video. You will learn how to configure the exact number of frames to loop (the size/frame count) and define the precise starting frame for the loop effect using practical command-line examples.

The FFmpeg loop filter allows you to repeat a specific segment of a video. To control the segment's length and where the loop begins, you must configure three primary parameters: loop, size, and start.

The Filter Parameters

  • loop: The number of times the selected sequence should repeat. Set this to -1 for infinite loops, or a positive integer (e.g., 3 to repeat three times).
  • size: The total number of frames in the loop sequence (the frame count).
  • start: The 0-based index of the first frame where the loop sequence begins.

Command Syntax

The basic syntax for applying the filter is:

ffmpeg -i input.mp4 -vf "loop=loop=N:size=S:start=F" -c:a copy output.mp4

Step-by-Step Configuration Example

Suppose you want to loop a segment of a 30 FPS video. You want the loop to start at the 5-second mark, last for 2 seconds, and repeat 3 times.

  1. Calculate the Start Frame (start): Multiplying the start time by the frame rate gives the start frame index.
    • 5 seconds × 30 FPS = Frame index 150.
  2. Calculate the Frame Count (size): Multiplying the duration of the loop by the frame rate gives the size.
    • 2 seconds × 30 FPS = 60 frames.
  3. Define the Loop Count (loop):
    • Set to 3 repetitions.

Using these values, your FFmpeg command will look like this:

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

Key Considerations

  • Audio Sync: The loop filter only affects video frames. To keep audio in sync or to loop audio, you must use additional audio filters like aloop. If audio sync is not required, use -an to disable audio, or copy the audio stream using -c:a copy.
  • Buffer Limit: The size parameter defines the size of the internal frame buffer. Setting an extremely high frame count can consume a significant amount of system memory.