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:

  • loop: The number of times to repeat the segment. Set this to -1 for infinite loops (requires defining an output duration), or a specific integer (e.g., 3 to repeat three times).
  • size: The number of frames to be looped.
  • start: The 0-based index of the first frame of the loop.

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.

  • loop: The number of loops. Set to -1 for infinite loops.
  • size: The number of samples to loop. You can also specify time units (e.g., 2s for two seconds) by appending 's'.
  • start: The starting sample or time of the loop.

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