Skip to content

Instantly share code, notes, and snippets.

@djmaze
Last active September 17, 2022 14:14
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save djmaze/5234008 to your computer and use it in GitHub Desktop.
Save djmaze/5234008 to your computer and use it in GitHub Desktop.
Serving Spotify audio through an MP3 stream via http (using Pulseaudio)
#!/usr/bin/env bash
#
# needs: Pulseaudio, VLC
# Load null sink module if not already loaded
pacmd list-sinks | grep steam 2>&1 >/dev/null
if [[ $? == 1 ]]; then
pactl load-module module-null-sink sink_name=steam;
fi
# Get index of running Spotify sink.
# Move over if existing.
INDEX=`pacmd list-sink-inputs | grep index: | awk -F': ' '{ print $2 }'`
if [[ $INDEX != '' ]]; then
echo "Spotify output found, moving to steam sink"
pactl move-sink-input $INDEX steam
else
echo "ERROR: No Spotify input found! Please change the output in the Pulseaudio mixer manually."
fi
# Get current IP address
IP=`/sbin/ifconfig | grep 'inet '| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`
tput setaf 2
echo "Serving output stream on: http://$IP:8080/pc.mp3"
echo "Stop with CTRL-C"
tput sgr0
# Start VLC, serving the sink output as MP3 stream
cvlc -q pulse://steam.monitor --sout "#transcode{acodec=mpga,ab=320,channels=2}:standard{access=http,dst=$IP:8080/pc.mp3}"
@stchris
Copy link

stchris commented Sep 19, 2014

Brilliant, thanks for this! The only semi-issue is if you have multiple IP addresses. But in that case it's hard to decide which one to pick without prompting the user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment