Stream PulseAudio Virtual Sink via FFmpeg
Capturing audio from a virtual sink in Linux allows you to route and broadcast specific application sound without mixing in your default microphone or desktop audio. This guide demonstrates how to create a permanent or temporary PulseAudio virtual sink, locate its monitor device, and use FFmpeg to stream that isolated audio to a remote server or save it to a file.
1. Create a PulseAudio Virtual Sink
First, you need to create a virtual sink (an empty audio output) where you can route the specific application audio you want to stream. Open your terminal and run the following command:
pactl load-module module-null-sink sink_name=Virtual_Stream sink_properties=device.description=Virtual_StreamThis command sets up a null sink named Virtual_Stream.
You can now open your system’s sound settings or use a tool like
pavucontrol (PulseAudio Volume Control) to route any
specific application’s playback into this newly created
Virtual_Stream.
2. Locate the Monitor Device Name
PulseAudio automatically creates a .monitor source for
every sink. To stream this audio, FFmpeg needs the exact name of this
monitor device. Run the following command to list your short source
names:
pactl list short sources | grep Virtual_StreamYou will see an output similar to
Virtual_Stream.monitor. This string is the exact device
identifier you will pass into FFmpeg.
3. Stream the Audio Using FFmpeg
Once you have the monitor name, you can use FFmpeg to capture the
live audio. FFmpeg utilizes the pulse input device to grab
the stream directly from the monitor source.
To stream the audio to an external RTMP server (like Twitch or YouTube), use the following template:
ffmpeg -f pulse -i Virtual_Stream.monitor -acodec aac -b:a 128k -f flv rtmp://your-server-url/live/stream-keyIf you prefer to test the capture locally by saving it to an MP3 file first, use this command:
ffmpeg -f pulse -i Virtual_Stream.monitor -acodec libmp3lame -b:a 192k output.mp34. Make the Virtual Sink Persistent (Optional)
The virtual sink created in step one will disappear when you reboot your system. If you want this virtual device to be available every time you boot, you can append the configuration to your PulseAudio settings.
Open the PulseAudio configuration file in a text editor:
nano ~/.config/pulse/default.paIf that file does not exist, open the system-wide configuration instead:
sudo nano /etc/pulse/default.paAdd the following lines to the bottom of the file:
.fail
load-module module-null-sink sink_name=Virtual_Stream sink_properties=device.description=Virtual_Stream
.nofail
Save and close the file. The virtual sink will now initialize automatically upon every user login.