How to Use FFmpeg AFIR Filter
The afir (Audio Finite Impulse Response) filter in
FFmpeg is a powerful tool used for applying impulse responses (IR) to
audio streams, enabling effects like convolution reverb, cabinet
simulation, and precise frequency equalization. This article provides a
quick, practical guide on how the afir filter works, its
basic syntax, and real-world command examples to help you implement it
in your audio processing projects.
Understanding the AFIR Filter
The afir filter works by convolving an input audio
signal with an impulse response file. An impulse response is a recording
of how a space (like a cathedral) or a piece of hardware (like a guitar
amplifier) reacts to a single spike of sound. By combining your source
audio with an IR, you can simulate that specific acoustic environment or
hardware coloration.
The filter requires at least two audio inputs: 1. Input 0: The original, dry audio stream. 2. Input 1: The impulse response (IR) audio file.
Basic Syntax and Command Structure
Because the afir filter requires multiple inputs, you
must use FFmpeg’s -filter_complex flag. Here is the basic
command template:
ffmpeg -i input.wav -i impulse_response.wav -filter_complex "[0:a][1:a]afir[out]" -map "[out]" output.wavIn this command: * -i input.wav is your dry audio. *
-i impulse_response.wav is the IR file. *
[0:a][1:a] feeds the audio of the first input and the audio
of the second input into the filter. * afir processes the
convolution. * -map "[out]" ensures the output of the
filter is saved to the final file.
Common Parameters
You can customize the behavior of the afir filter by
passing key-value parameters.
dry: Sets the gain of the dry (unprocessed) signal. Default is 1.0.wet: Sets the gain of the wet (processed) signal. Default is 1.0.gweight: Sets the global gain weight. This is useful for preventing digital clipping if the output signal becomes too loud.irgain: Adjusts the gain of the impulse response before processing.
Practical Examples
1. Adjusting the Wet/Dry Balance
To create a natural-sounding reverb, you usually want more of the dry
signal and less of the wet signal. You can adjust this using the
dry and wet parameters:
ffmpeg -i dry_vocal.mp3 -i room_ir.wav -filter_complex "[0:a][1:a]afir=dry=10:wet=0.5[out]" -map "[out]" output.mp32. Preventing Audio Clipping
Convolution can significantly increase the volume of your output,
resulting in unpleasant distortion. You can apply the
gweight parameter to scale down the overall volume:
ffmpeg -i guitar.wav -i cab_ir.wav -filter_complex "[0:a][1:a]afir=gweight=0.5[out]" -map "[out]" processed_guitar.wav3. Working with Stereo Signals
If your input is stereo and your IR file is stereo, the
afir filter will automatically map the left channel of the
IR to the left channel of the input, and the right channel of the IR to
the right channel of the input. Ensure both files share the same sample
rate (e.g., 44100 Hz or 48000 Hz) for the best results, as FFmpeg will
automatically resample the IR to match the input if they do not
match.