How to Configure Loop Duration in FFmpeg Loop Filter
This article provides a quick overview and practical guide on how to
configure the loop duration using the FFmpeg loop filter.
You will learn how the filter utilizes frame counts to define duration,
how to calculate the correct parameters based on your video’s frame
rate, and how to apply these settings using clear, real-world
command-line examples.
Understanding the FFmpeg Loop Filter Parameters
The FFmpeg loop video filter does not accept duration
directly in seconds. Instead, it requires you to specify the loop
duration in frames. To configure the filter, you must
use three primary parameters:
loop: The number of times the identified segment should repeat. Set this to-1for infinite looping.size: The number of frames to be looped (this defines the duration of the loop).start: The frame number where the loop begins (0-based index).
Calculating Loop Duration in Frames
To set a specific duration in seconds, you must multiply your desired duration by the frame rate (FPS) of your input video:
\[\text{size (frames)} = \text{duration (seconds)} \times \text{FPS}\]
For example, if you want to loop a 5-second segment of a video that runs at 30 FPS, your calculation is:
\[5 \times 30 = 150 \text{ frames}\]
Therefore, your size parameter will be
150.
Practical Command Examples
Example 1: Loop the Beginning of a Video
To loop the first 3 seconds of a 24 FPS video 5 times, calculate the frame size (3 seconds × 24 FPS = 72 frames) and start at frame 0:
ffmpeg -i input.mp4 -vf "loop=loop=5:size=72:start=0" output.mp4Example 2: Loop a Segment in the Middle of a Video
To loop a 4-second segment that starts 10 seconds into a 30 FPS
video, calculate the start frame and the size: * Start
Frame: 10 seconds × 30 FPS = frame 300 *
Size (Duration): 4 seconds × 30 FPS = 120
frames
Use the following command to loop this segment 3 times:
ffmpeg -i input.mp4 -vf "loop=loop=3:size=120:start=300" output.mp4Example 3: Infinite Looping for GIF/Stream Output
If you are outputting to a format that supports infinite playback,
such as a GIF, or preparing a stream, set the loop
parameter to -1. To infinitely loop the first 150 frames (5
seconds at 30 FPS):
ffmpeg -i input.mp4 -vf "loop=loop=-1:size=150:start=0" output.gif