Load Impulse Response Files in FFmpeg AFIR Filter
This article provides a straightforward guide on how to load and
apply impulse response (IR) files using the FFmpeg afir
filter. You will learn the correct command-line syntax for combining
your source audio with an IR file, handling sample rate mismatches, and
adjusting filter parameters like gain and wet/dry mix to achieve optimal
convolution results.
The Basic Command Structure
To apply an impulse response to an audio file, you must feed both the
main audio track and the IR audio file into FFmpeg. Because the
afir filter requires two distinct input streams, you must
use filter_complex to route them correctly.
Here is the standard 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 the main audio source
(Stream 0). * -i impulse_response.wav is the IR file
(Stream 1). * [0:a][1:a]afir takes the audio from the first
input and the audio from the second input, feeding them into the
afir filter. * -map "[out]" maps the processed
output to the final file.
Handling Sample Rate Mismatches
The afir filter requires both the input audio and the
impulse response file to have the exact same sample rate. If they do not
match, FFmpeg will throw an error. To prevent this, resample the impulse
response stream before passing it to the filter:
ffmpeg -i input.wav -i impulse_response.wav -filter_complex "[1:a]aresample=async=1:min_comp=0.01:comp_delta=200:max_soft_comp=100000000[ir];[0:a][ir]afir[out]" -map "[out]" output.wavAlternatively, you can use a simpler resampling chain if you know the target rate:
ffmpeg -i input.wav -i impulse_response.wav -filter_complex "[1:a]aresample=resampler=soxr[ir];[0:a][ir]afir[out]" -map "[out]" output.wavControlling Gain and Volume
Convolution often increases the overall signal level, which can cause
digital clipping. The afir filter includes a gain
(g) parameter to manage this.
- Automatic Gain Reduction: Set
g=minmaxto automatically analyze the IR and apply volume attenuation to prevent clipping. - Manual Gain: Set a specific decibel value, such as
g=-6dBor a linear scale value likeg=0.5.
ffmpeg -i input.wav -i impulse_response.wav -filter_complex "[0:a][1:a]afir=g=minmax[out]" -map "[out]" output.wavAdjusting Wet and Dry Mix
By default, the afir filter outputs only the wet
(processed) signal. If you are simulating a space (reverb) and want to
keep some of the original, unaffected audio, you can adjust the
dry and wet parameters. The values are
specified in decibels:
ffmpeg -i input.wav -i impulse_response.wav -filter_complex "[0:a][1:a]afir=dry=-6dB:wet=-12dB[out]" -map "[out]" output.wavSetting dry=0dB keeps the original audio at full volume,
while wet=-12dB adds a subtle layer of the impulse response
effect.