Configure FFmpeg aloop Filter Loop Duration

This article provides a quick guide on how to configure the loop duration when using the aloop audio filter in FFmpeg. You will learn how to calculate the correct buffer size based on your audio’s sample rate to achieve precise loop lengths, along with practical command-line examples to implement these settings.

The FFmpeg aloop filter loops a segment of input audio. Unlike some video filters that accept duration in seconds, the aloop filter defines the loop duration using the number of audio samples. To configure the loop duration, you must define three main parameters: loop, size, and start.

The Core Parameters

How to Calculate Loop Duration in Samples

Because the size parameter requires samples instead of seconds, you must calculate the value using the sample rate of your input audio (commonly 44,100 Hz or 48,000 Hz).

Use this formula to find the correct size value:

\[\text{Size (Samples)} = \text{Desired Duration (Seconds)} \times \text{Sample Rate (Hz)}\]

Example Calculations:

Practical FFmpeg Commands

Here is how to apply these calculations in a standard FFmpeg command.

Loop a 5-second clip 3 times (assuming 44.1 kHz audio):

ffmpeg -i input.wav -filter_complex "aloop=loop=3:size=220500:start=0" output.wav

Loop a 10-second clip infinitely (assuming 48 kHz audio):

ffmpeg -i input.wav -filter_complex "aloop=loop=-1:size=480000:start=0" output.wav

Determining Your Audio Sample Rate

If you do not know the sample rate of your input file, you can find it using ffprobe:

ffprobe -v error -select_streams a:0 -show_entries stream=sample_rate -of default=noprint_wrappers=1 input.wav

This command will output the sample rate (e.g., sample_rate=48000), which you can then plug into the formula to configure your loop duration.