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:

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.mp4

Example 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.mp4

Example 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