How to Output Audio to PulseAudio with FFmpeg
This article provides a straightforward guide on how to route audio playback from FFmpeg directly to a PulseAudio sound server. You will learn the essential command-line syntax, how to stream audio in real-time, and how to target specific PulseAudio playback devices (sinks) on your system.
Basic Command Syntax
To output audio to PulseAudio, you must use FFmpeg’s
pulse muxer by specifying the -f pulse format
option. The simplest command to play an audio file through your default
PulseAudio device is:
ffmpeg -i input.mp3 -f pulse "Playback Stream Name"In this command: * -i input.mp3 specifies your input
audio file. * -f pulse forces FFmpeg to use the PulseAudio
output driver. * "Playback Stream Name" is an arbitrary
label that will appear in your PulseAudio volume control (e.g.,
pavucontrol) to identify the stream.
Playing Audio in Real-Time
By default, FFmpeg processes files as fast as possible. If you are
playing an audio file to your speakers, you must force FFmpeg to read
the input at its native frame rate. To do this, use the -re
(real-time) input flag:
ffmpeg -re -i input.mp3 -f pulse "My Live Stream"Failing to use the -re option may result in stuttering,
buffer underruns, or the audio playing back too quickly.
Outputting to a Specific PulseAudio Device
If you have multiple sound cards, USB headsets, or Bluetooth speakers, you can direct the FFmpeg stream to a specific PulseAudio “sink.”
First, list your available PulseAudio sinks by running this command in your terminal:
pactl list short sinksIdentify the name of the device you want to use (for example,
alsa_output.pci-0000_00_1f.3.analog-stereo).
Next, use the -device option in FFmpeg to target that
specific device:
ffmpeg -re -i input.mp3 -f pulse -device "alsa_output.pci-0000_00_1f.3.analog-stereo" "Direct Stream"Mixing Audio and Video
If you are playing a video file but only want to send the audio track
to PulseAudio (while ignoring or discarding the video), you can disable
the video output using the -vn flag:
ffmpeg -re -i video.mp4 -vn -f pulse "Video Audio"