How to Configure Loop Count in FFmpeg aloop Filter
This guide explains how to configure the loop count using the FFmpeg
aloop audio filter. You will learn the exact syntax, the
parameters required to control how many times an audio segment repeats,
and how to set up both finite and infinite loops for your audio
processing workflows.
Understanding the aloop Filter Parameters
To configure the loop count in the aloop filter, you
must define the loop parameter alongside other required
parameters like size and start. The
aloop filter buffers a segment of incoming audio and plays
it back repeatedly based on your configuration.
The three key parameters are:
loop: The number of times to loop the buffered audio.- Set to
0for no looping (default). - Set to any positive integer (e.g.,
5) to loop exactly that many times. - Set to
-1for an infinite loop.
- Set to
size: The maximum size of the loop buffer in samples. This parameter is mandatory. For example, for 5 seconds of 44.1 kHz audio, the size must be at least220500samples (5 * 44100).start: The sample number where the loop buffer starts recording. Usually set to0to start buffering from the beginning of the audio.
Command Examples
Example 1: Loop Audio 3 Times
To loop the first 2 seconds of a 44.1 kHz audio file 3 times, use the following command:
ffmpeg -i input.wav -filter_complex "aloop=loop=3:size=88200:start=0" output.wavIn this command: * loop=3 repeats the buffered audio 3
times. * size=88200 allocates enough buffer for 2 seconds
of audio at 44,100 Hz. * start=0 begins buffering at the
very start of the input file.
Example 2: Infinite Loop
To loop the buffered segment indefinitely, set the loop
parameter to -1. Note that when using infinite loops, you
should specify an output duration limit (using -t or
-to) so the command eventually stops running:
ffmpeg -i input.wav -filter_complex "aloop=loop=-1:size=88200:start=0" -t 30 output.wavThis command loops the first 2 seconds of the input infinitely but
stops the encoding process and saves the file once the output file
reaches a duration of 30 seconds (-t 30).