How to Use sndio with FFmpeg on OpenBSD

This article provides a practical guide on utilizing the native OpenBSD audio subsystem, sndio, with the FFmpeg multimedia framework. It covers the specific command-line syntax required to capture audio from your microphone (input) and play audio streams back through your speakers or headphones (output) using sndio on an OpenBSD system.

To begin, ensure your FFmpeg installation on OpenBSD has sndio support enabled. You can verify this by listing the available devices in your terminal:

ffmpeg -devices

Look for sndio in the output list. It should be marked with D (demuxing/input support) and E (muxing/output support).

Recording Audio (Input)

To capture audio using sndio as an input device, use the -f sndio format flag, followed by the device identifier. On OpenBSD, the default audio device is designated as default.

To record audio from your default microphone to an MP3 file, run:

ffmpeg -f sndio -i default output.mp3

To specify the sample rate and the number of audio channels during the capture process, add the -ar (audio rate) and -ac (audio channels) flags before the input:

ffmpeg -f sndio -ar 44100 -ac 2 -i default output.wav

Playing Audio (Output)

To play an audio file directly through your system speakers using FFmpeg and sndio, direct the output to the sndio muxer and target the default device.

Run the following command for playback:

ffmpeg -i input.mp3 -f sndio default

For video files, if you only want to play the audio track through sndio without rendering the video stream, disable video processing using the -vn flag:

ffmpeg -i video.mp4 -vn -f sndio default

Specifying Custom Devices

If you have multiple sound cards or USB audio interfaces, you can target them by replacing default with the specific sndio device name (such as snd/1 for the second audio device):

# Record from the second audio device
ffmpeg -f sndio -i snd/1 output.wav

# Play to the second audio device
ffmpeg -i input.wav -f sndio snd/1