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:

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.mp3

Example 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.wav

Important Usage Tips