How to Use the FFmpeg aloop Filter
This article provides a quick overview and practical guide on how to
use the aloop filter in FFmpeg to loop audio segments. You
will learn the core syntax of the filter, understand how to calculate
audio samples for precise looping, and see step-by-step command examples
for looping audio a set number of times or indefinitely.
The aloop filter in FFmpeg allows you to loop a specific
segment of an audio stream. Unlike video looping, which often works with
frames, aloop works directly with audio samples.
The Basic Syntax
The basic syntax for the aloop filter is:
-af "aloop=loop=loop_count:size=sample_count:start=start_sample"The filter requires three primary parameters:
loop: The number of times the audio segment should repeat. Set this to-1for an infinite loop,0to disable looping, or any positive integer (e.g.,5to loop five times).size: The length of the audio segment you want to loop, measured in audio samples.start: The starting point of the loop, also measured in audio samples.
How to Calculate Audio Samples
Because aloop requires the size and
start values in samples rather than seconds, you must
calculate these values using your audio’s sample rate (usually 44,100 Hz
or 48,000 Hz).
Use this formula: \[\text{Samples} = \text{Seconds} \times \text{Sample Rate}\]
For example, if your audio sample rate is 44,100 Hz: * To loop a
5-second segment: \(5 \times
44,100 = 220,500\) samples for the size. * To start
the loop at 2 seconds in: \(2
\times 44,100 = 88,200\) samples for the start.
Practical Examples
Example 1: Loop the first 5 seconds of an audio file 3 times
Assuming a sample rate of 44,100 Hz, the size is 220,500 samples, and the start is 0.
ffmpeg -i input.mp3 -af "aloop=loop=3:size=220500:start=0" output.mp3Example 2: Loop a specific section indefinitely
To loop a 10-second segment starting at the 3-second mark of a 48,000 Hz audio file forever: * Start: \(3 \times 48,000 = 144,000\) samples * Size: \(10 \times 48,000 = 480,000\) samples
ffmpeg -i input.wav -af "aloop=loop=-1:size=480000:start=144000" output.wavImportant Usage Tips
- Input Duration: If you use an infinite loop
(
loop=-1), the output stream will theoretically run forever. To prevent FFmpeg from rendering indefinitely, limit the output duration using the-tparameter (e.g.,-t 30to limit the output to 30 seconds). - Finding Sample Rate: If you do not know the sample
rate of your input file, you can find it using the command
ffprobe -v error -select_streams a:0 -show_entries stream=sample_rate -of default=noprint_wrappers=1 input.mp3.