FFmpeg Loop Filter: Frame Count and Start Frame

This guide explains how to control video looping in FFmpeg using the loop video filter. By configuring the filter’s parameters, you can select an exact starting frame, determine the number of consecutive frames to repeat, and set how many times the sequence loops.

Understanding the loop Filter Syntax

The FFmpeg loop filter uses three primary parameters to control playback:

  • loop: The number of times to repeat the specified sequence. A value of -1 loops indefinitely, 0 disables looping, and any positive integer N loops the segment N additional times.
  • size: The total number of frames in the loop sequence (the frame count).
  • start: The index of the first frame where the loop begins, using a zero-based index (e.g., 0 is the first frame of the video).

The basic filter syntax is:

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

Practical Examples

Loop a Specific Section Multiple Times

To loop a 60-frame segment starting at frame 120 three additional times:

ffmpeg -i input.mp4 -vf "loop=loop=3:size=60:start=120" output.mp4

In this command:

  • start=120 begins the loop segment at the 121st frame.
  • size=60 captures a 60-frame segment (frames 120 through 179).
  • loop=3 plays this 60-frame segment 4 times in total (1 original play + 3 loops) before continuing with the rest of the video.

Loop from the Beginning of the Video

To repeat the first 100 frames twice:

ffmpeg -i input.mp4 -vf "loop=loop=2:size=100:start=0" output.mp4

Loop a Segment Indefinitely

To create an infinite loop of a 30-frame sequence starting at frame 50:

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

Note: Infinite loops run until manually stopped unless constrained by an output option like -t <duration> or -frames:v <count>.

Handling Audio

The loop filter only applies to the video stream. If your input file has audio, the audio will continue playing normally while the video loops, causing desynchronization. To loop audio alongside video, you must apply the aloop filter to the audio stream with sample-based parameters:

ffmpeg -i input.mp4 -vf "loop=loop=2:size=60:start=120" -af "aloop=loop=2:size=88200:start=176400" output.mp4

Alternatively, you can discard audio during testing by adding the -an flag:

ffmpeg -i input.mp4 -vf "loop=loop=2:size=60:start=120" -an output.mp4