How to Use FFmpeg aloop Filter to Loop Audio
This article provides a quick guide on how to use the
aloop filter in FFmpeg to repeat or loop specific audio
segments. You will learn the core parameters of the filter—such as loop
count, size, and start position—and see practical command-line examples
to help you loop your audio files seamlessly.
The aloop filter works by buffering a segment of audio
and repeating it a set number of times. To use this filter effectively,
you must define three primary parameters:
loop: The number of times the audio segment should repeat. Set this to-1for infinite looping, or a positive integer (e.g.,3) for a specific number of repetitions. The default is0(no loop).size: The maximum size of the loop segment, measured in samples. To calculate the sample size, multiply the audio’s sample rate (usually 44,100 Hz or 48,000 Hz) by the duration in seconds. For example, a 5-second loop at 44.1 kHz requires a size of220500samples (5 * 44,100).start: The starting sample number where the loop should begin. To start the loop at the very beginning of the audio file, set this to0.
Example 1: Loop the First 5 Seconds of Audio Three Times
Assuming your input audio has a sample rate of 44,100 Hz, a 5-second segment equals 220,500 samples. To loop this segment 3 times from the beginning of the file, use the following command:
ffmpeg -i input.mp3 -filter_complex "aloop=loop=3:size=220500:start=0" output.mp3Example 2: Loop a Specific Segment in the Middle of the Audio
To loop a specific 4-second segment that starts at the 2-second mark of a 44,100 Hz audio file, you must calculate the start and size in samples:
- Start sample (at 2 seconds): 2 * 44,100 =
88200 - Size (4 seconds long): 4 * 44,100 =
176400
To loop this specific segment twice, run:
ffmpeg -i input.mp3 -filter_complex "aloop=loop=2:size=176400:start=88200" output.mp3Example 3: Infinite Looping
If you want to loop a 10-second segment (441,000 samples at 44.1 kHz)
indefinitely, set the loop count to -1. Note that infinite
loops are typically used when streaming or pipe-outputting audio, as
saving to a static file requires a finite duration.
ffmpeg -i input.mp3 -filter_complex "aloop=loop=-1:size=441000:start=0" -f mp3 output.mp3