Play Audio Directly Through ALSA Using FFmpeg
This article explains how to output audio directly to the Advanced
Linux Sound Architecture (ALSA) driver using FFmpeg’s alsa
muxer. You will learn the correct command-line syntax, how to identify
and specify your playback hardware, and how to configure key audio
parameters for seamless direct playback without the need for a heavy
desktop audio server.
Prerequisites
To play audio directly through ALSA, your FFmpeg installation must be compiled with ALSA support enabled. You can verify this by running:
ffmpeg -muxers | grep alsaIf you see E alsa ALSA audio output in the list, your
FFmpeg build is ready.
Basic Playback Command
To stream any audio or video file directly to your default ALSA
playback device, use the -f alsa output format option
followed by the device name:
ffmpeg -i input.mp3 -f alsa defaultIn this command: * -i input.mp3 specifies your source
file (which can be any audio or video format FFmpeg supports). *
-f alsa forces the output format to use the ALSA driver. *
default directs the stream to your system’s default ALSA
soundcard.
Selecting a Specific ALSA Device
If you have multiple sound cards (such as an onboard chip, a USB DAC, or HDMI output), you can direct the audio to a specific hardware device.
First, list your available ALSA playback devices by running:
aplay -LLook for device names like hw:CARD=PCH,DEV=0 or
simplified hardware identifiers like hw:0,0 (representing
card 0, device 0).
To play audio through a specific card and device, replace
default with the hardware identifier:
ffmpeg -i input.wav -f alsa hw:0,0Using hw:X,Y provides direct, unmixed hardware access.
If you want to use ALSA’s software mixing (so other applications can
play sound at the same time), use plughw instead:
ffmpeg -i input.wav -f alsa plughw:0,0Controlling Audio Parameters
Sometimes, your hardware may not natively support the sample rate or channel layout of your source file. You can use FFmpeg’s built-in options to resample the audio on the fly before sending it to ALSA:
ffmpeg -i input.flac -ar 44100 -ac 2 -f alsa hw:0,0-ar 44100sets the output audio sample rate to 44.1 kHz.-ac 2forces the channel layout to stereo (2 channels).