How to Use the FFmpeg Loop Filter

This article provides a quick guide on how to use the loop filter in FFmpeg to repeat specific segments of video or audio. You will learn the basic syntax, key parameters such as loop count, size, and start position, and practical command-line examples to successfully loop your media files.

The Video Loop Filter (loop)

To loop a specific segment of a video, you use the video filter option (-vf) with the loop filter. The filter requires three main parameters:

Basic Video Example

To loop 100 frames starting from frame 300, repeating them 5 times:

ffmpeg -i input.mp4 -vf "loop=loop=5:size=100:start=300" output.mp4

Infinite Video Loop with Duration Limit

If you want to loop a segment infinitely, you must set the loop parameter to -1 and specify a total output duration using the -t option so the encoding process stops:

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

This command loops the first 150 frames of the video continuously for a total output duration of 30 seconds.


The Audio Loop Filter (aloop)

To loop audio, you must use the audio filter version, which is aloop. It functions similarly to the video filter but operates on audio samples instead of video frames.

Basic Audio Example

To loop a 2-second segment of audio starting at the 5-second mark, repeating it 3 times:

ffmpeg -i input.wav -af "aloop=loop=3:size=2s:start=5s" output.wav