Use FFmpeg afir Filter for Audio Convolution
This article provides a practical guide on how to apply a convolver
filter to an audio stream using FFmpeg’s afir (Audio Finite
Impulse Response) filter. You will learn how to combine a primary audio
track with an Impulse Response (IR) file to recreate acoustic spaces,
cabinet simulations, or custom filter effects, along with the exact
command-line syntax required.
Understanding the
afir Filter
The afir filter in FFmpeg performs frequency-domain
convolution. It takes two audio inputs: the primary audio signal you
want to process, and a second audio stream containing the Impulse
Response (IR). The IR represents the acoustic characteristics of a space
(like a cathedral or a studio) or a piece of hardware (like a guitar
amplifier cabinet).
Basic Command Syntax
To apply the convolution filter, you must pass both the target audio
and the IR file as inputs, and then link them using
filter_complex.
Here is the standard command structure:
ffmpeg -i input.wav -i impulse_response.wav -filter_complex "[0:a][1:a]afir=ginit=1[out]" -map "[out]" output.wavCommand Breakdown:
-i input.wav: The primary audio stream (input 0).-i impulse_response.wav: The audio file containing the impulse response (input 1).-filter_complex: Used because the filter requires multiple input streams.[0:a][1:a]: Specifies that the first input’s audio (0:a) and the second input’s audio (1:a) are fed into the filter.afir=ginit=1: Calls the FIR filter. Theginitparameter sets the initial gain to prevent volume clipping.-map "[out]": Maps the filtered output to the final destination file.
Adjusting Dry and Wet Mix
By default, the afir filter outputs only the wet (fully
processed) signal. If you want to mix the original “dry” audio with the
“wet” convolved audio, you can use the dry and
wet parameters.
ffmpeg -i input.wav -i impulse_response.wav -filter_complex "[0:a][1:a]afir=dry=10:wet=10[out]" -map "[out]" output.wavIn this command, the values are represented in decibels (dB). Setting
both to 10 (or 0 for no change) allows you to
balance the level of the original sound against the strength of the
convolution effect.
Handling Different Sample Rates
For the afir filter to work correctly, both the input
audio and the impulse response file should share the same sample rate.
If they do not, you can resample the impulse response on the fly using
the aresample filter:
ffmpeg -i input.wav -i impulse_response.wav -filter_complex "[1:a]aresample=48000[ir];[0:a][ir]afir=ginit=1[out]" -map "[out]" output.wavIn this setup, the second input (the IR) is resampled to 48,000 Hz
before being passed to the afir filter alongside the
primary audio.