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

  • loop: The number of times the segment should loop. Set this to -1 for infinite loops, 0 to disable looping, or any positive integer (e.g., 5 to loop five times).
  • size: The maximum size of the loop buffer in samples. This parameter directly controls the duration of the audio loop.
  • start: The starting sample number where the loop begins (usually set to 0 to start from the beginning of the audio).

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:

  • To loop a 5-second segment of a 44.1 kHz (44100 Hz) audio file: \[5 \times 44100 = 220,500\text{ samples}\] Your filter configuration: aloop=loop=5:size=220500:start=0

  • To loop a 10-second segment of a 48 kHz (48000 Hz) audio file: \[10 \times 48000 = 480,000\text{ samples}\] Your filter configuration: aloop=loop=5:size=480000:start=0

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.