FFmpeg Aresample Audio Rate Conversion

The aresample filter in FFmpeg is the primary tool for changing the sample rate, channel layout, and sample format of audio streams. This article explains how this filter handles sample rate conversions under the hood, exploring its default resampling engines, mathematical interpolation methods, and how it manages audio-to-video synchronization through dynamic sample rate adjustments.

Resampling Engines: Swr vs. SoX

Under the hood, the aresample filter acts as a wrapper for two primary resampling libraries: libswresample (the default FFmpeg resampler) and libsoxr (the SoX Resampler library).

You can specify the engine using the resampler option: -af aresample=resampler=soxr

The Mathematical Process: Anti-Aliasing and Interpolation

When converting from a source sample rate (e.g., 44.1 kHz) to a target sample rate (e.g., 48 kHz), aresample performs three primary mathematical operations:

  1. Up-sampling / Down-sampling: The filter inserts zero-valued samples between existing samples (upsampling) or discards samples (downsampling) to approximate the target rate.
  2. Low-Pass Filtering: To prevent aliasing (where high-frequency sounds fold down into lower, audible frequencies), a Kaiser-windowed sinc low-pass filter is applied. By default, the cutoff frequency is set slightly below the Nyquist frequency of the lower of the two sample rates.
  3. Polyphase Filtering: Because exact integer ratios between rates (like 44100 and 48000) would require massive filter sizes, aresample uses a polyphase filterbank. It interpolates between the closest calculated samples using a configurable number of phases (controlled by the phase_shift parameter) to determine the exact amplitude of the new sample.

Managing Audio Synchronization (Async and Compensation)

In addition to static rate conversion, aresample is crucial for maintaining audio-to-video synchronization. When audio packets arrive with timestamps that do not match the expected timeline, aresample can dynamically stretch or squeeze the audio.

Common Configuration Parameters

To fine-tune how aresample handles your audio, you can adjust several key parameters in your filter string: