GENERAL
This document describes how a local program that plays to its speakers can play to a local icecast server and thereby to audio streamers in your network. I discussed this with chatgpt in a session called: Using audio output across programs across your network. https://chatgpt.com/c/697630ec-c84c-832a-8163-8b94be8151c8
- Your Linux apps playing audio → PulseAudio sink For example: audacious
- Encoding (OGG/MP3) → streams to Icecast For example: darkice or ffmpeg
- Icecast source client records the sink’s monitor Icecast sends it over your network
Icecast
You must first install, configure and start icecast:
sudo apt-get install icecast
make a icecast configuration or edit an existing one for a mountpoint:
<mount>
<mount-name>/system.mp3</mount-name>
<username>source</username>
<password>hackme</password>
<public>1</public>
<max-listeners>100</max-listeners>
</mount>
enable icecast:
sudo systemctl daemon-reload
sudo systemctl enable icecast2
sudo systemctl start icecast2
Encoding
ffmpeg -f pulse -i default \
-acodec libmp3lame -b:a 128k \
-f mp3 icecast://source:hackme@localhost:8000/system.mp3
Coupling
On the command line:
ID=$(pactl list source-outputs | \
awk '
/Source Output #/ {id=$3}
/application.process.binary = "ffmpeg"/ {print id; exit}
')
[ -n "$ID" ] && pactl move-source-output "$ID" "$MONITOR"
This does what pavucontrol does with a graphical interface.
The script stream-to-icecast.sh
#!/bin/sh
#See output of: pactl list short sources
SINK="alsa_output.pci-0000_00_1b.0.analog-stereo"
MONITOR="${SINK}.monitor"
#Start ffmpeg
/usr/bin/ffmpeg -f pulse -i default \
-acodec libmp3lame -b:a 128k \
-f mp3 icecast://source:hackme@localhost:8000/system.mp3 &
FFPID=$!
#Give PulseAudio time to register the source-output
sleep 2
#Find ffmpeg source-output ID and move it
ID=$(pactl list source-outputs | \
awk '
/Source Output #/ {id=$3}
/application.process.binary = "ffmpeg"/ {print id; exit}
')
[ -n "$ID" ] && pactl move-source-output "$ID" "$MONITOR"
wait "$FFPID"
Systemd
If you want to make this automatically start with systemd:
make a unit file ~/.config/systemd/user/stream-to-icecast.service:
Description=Stream system audio to Icecast (MP3)
After=pulseaudio.service
Wants=pulseaudio.service
[Service]
ExecStart=/home/rocus/.local/bin/stream-to-icecast.sh
Restart=always
RestartSec=5
[Install]
WantedBy=default.target
then do:
systemctl --user daemon-reload
systemctl --user enable stream-to-icecast.service
systemctl --user start stream-to-icecast.service
systemctl --user status stream-to-icecast.service
I use Debian 12. Things like places of files maybe different on your system. ie. systemd user files.