How to Create Binaural Audio with FFmpeg

This article explains how to use FFmpeg’s specialized audio spatialization filters to convert multi-channel audio into a 3D binaural stereo stream. You will learn the exact command-line syntax, how to map audio channels, and how to utilize Head-Related Impulse Response (HRIR) or SOFA files to achieve an immersive, spatialized headphone listening experience.

Understanding Binaural Filters in FFmpeg

While FFmpeg does not have a filter named literally binaural, it achieves binaural synthesis using two primary filters: headphone and sofalizer. These filters apply Head-Related Transfer Functions (HRTF) to multi-channel audio (like 5.1 or 7.1 surround sound), processing it so that a listener wearing standard stereo headphones perceives sound coming from specific directions in 3D space.

Method 1: Using the headphone Filter (HRIR WAV method)

The headphone filter is the most common way to create binaural audio in FFmpeg. It requires two inputs: your multi-channel audio track and a multi-channel HRIR (Head-Related Impulse Response) WAV file.

Step-by-Step Command

To convert a 5.1 surround sound file into a binaural stereo file, use the following command structure:

ffmpeg -i input_51.wav -i hrir.wav -filter_complex "[0:a][1:a]headphone=map=0|1|2|3|4|5[out]" -map "[out]" output_binaural.wav

Command Breakdown:

Method 2: Using the sofalizer Filter (SOFA method)

If you have a .sofa (Spatially Oriented Format for Acoustics) file instead of an HRIR WAV file, you should use the sofalizer filter. This format contains highly accurate, standardized 3D audio measurements.

Step-by-Step Command

Run this command to apply a .sofa file directly to a multi-channel source:

ffmpeg -i input_51.wav -af "sofalizer=sofa=/path/to/spatial_profile.sofa" output_binaural.wav

Command Breakdown:

Advanced Customization (Optional)

You can customize the virtual speaker positions and gain inside the sofalizer filter:

ffmpeg -i input_51.wav -af "sofalizer=sofa=/path/to/spatial_profile.sofa:gain=3:rotation=90" output_binaural.wav