How to Use FFmpeg aloop Filter to Repeat Audio
This article explains how to use the FFmpeg aloop filter
to repeat a specific segment of an audio file. You will learn the key
parameters of the filter, how to calculate the required sample values
based on your audio’s sample rate, and how to structure the command-line
syntax for seamless audio looping.
Understanding the aloop Filter
The aloop filter works by buffering a specific segment
of audio and repeating it a set number of times. Unlike video looping,
aloop operates using audio samples rather
than timecodes (seconds).
To use the filter, you need to configure three primary parameters: *
loop: The number of times to repeat the
segment. Set this to -1 for infinite loops, or a specific
integer (e.g., 3 for three loops). *
size: The length of the audio segment to
loop, measured in samples. * start: The
starting position of the loop, measured in samples.
Step 1: Calculate Samples from Time
Because aloop requires sample counts, you must convert
your target times (in seconds) into samples using the audio’s sample
rate.
\[\text{Samples} = \text{Time in seconds} \times \text{Sample Rate (Hz)}\]
Common sample rates are 44,100 Hz (CD quality) or 48,000 Hz (video/professional audio). You can find your file’s sample rate by running:
ffprobe -v error -select_streams a:0 -show_entries stream=sample_rate -of default=noprint_wrappers=1 input.mp3Calculation Example
Assume your audio file has a sample rate of 48,000 Hz, and you want to loop a 3-second segment that starts at the 5-second mark:
- Start Sample (
start): 5 seconds × 48,000 = 240,000 - Loop Size (
size): 3 seconds × 48,000 = 144,000
Step 2: Write the FFmpeg Command
Once you have calculated the values, insert them into the
aloop filter graph.
Basic Syntax
ffmpeg -i input.mp3 -filter_complex "aloop=loop=3:size=144000:start=240000" output.mp3In this command: * -i input.mp3: Specifies the input
audio file. * -filter_complex: Invokes the filtergraph. *
loop=3: Repeats the target segment 3 times. *
size=144000: Loops a segment duration of 144,000 samples (3
seconds). * start=240000: Starts the loop at sample 240,000
(5-second mark).
Advanced Usage: Infinite Looping with a Limit
If you set loop=-1 for an infinite loop, the output
generation will run indefinitely. To prevent this, you must specify a
maximum output duration or limit the output frames using the
-t (duration) or -to option.
For example, to loop the segment infinitely but cut the final output file off at 30 seconds:
ffmpeg -i input.mp3 -filter_complex "aloop=loop=-1:size=144000:start=240000" -t 30 output.mp3