Skip to content

Instantly share code, notes, and snippets.

@kmwenja
Last active December 6, 2023 06:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kmwenja/68443267e301efbbf5ca6a678ec9cd15 to your computer and use it in GitHub Desktop.
Save kmwenja/68443267e301efbbf5ca6a678ec9cd15 to your computer and use it in GitHub Desktop.

MPV Radio

A bash script that turns mpv into a radio.

Requirements

  • mpv: to play the stream
  • dmenu: to choose radio stations
  • jq: to parse IPC responses from mpv
  • socat: to send IPC requests to mpv
  • notify-send: to trigger desktop notifications
  • awk: string processing
  • youtube-dl: to play some streams

How it works:

Create a file at $HOME/.radio/stations that will contain your online radio stations. This is how you'll control what stations you can listen to. Each station should be recorded on its own line in the file as "<station name>",<station_url>. Here's an example (using fake stations):

"Metal.FM",https://metal.fm
"HipHop 90s",https://anothersite.com/hiphop/90s/

You can also run radio add <station name> <station url> e.g. radio add "Metal.FM" "https://metal.fm"

Once a list of stations has been prepped, run radio start. This will launch dmenu asking you to select one of your radio stations. Once the station is selected, the script launches mpv in the background to play the station stream. You can run radio start again to change stations. The mpv instance is reused every time you run radio start to change stations (so long as it's still alive).

To stop mpv playing the stream, run radio stop.

To find out the current song that's playing on the stream (assuming the station provides icy-title tags for the songs its playing), run radio info.

You can also mute/unmute the stream with radio toggle.

Running radio with no arguments will print out a usage guide.

Have fun!

LICENSE

MIT License

Copyright (c) 2019 Kennedy Mwenja

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#!/bin/bash
RADIOPID=$HOME/.radio/mpv.pid
STATIONSFILE=$HOME/.radio/stations
RADIOSOCKET=$HOME/.radio/mpv.sock
function rstart {
# select the radio station to start playing
STATION_NAME=$(cat $STATIONSFILE | awk -F ',' '{print $1}' | dmenu -i -p 'Choose station:')
if [ $? -eq 1 ]; then
echo "No station selected"
exit 1
fi
STATION_URL=$(grep "$STATION_NAME" $STATIONSFILE | awk -F ',' '{print $2}')
if [ $? -eq 1 ]; then
echo "'$STATION_NAME' was not found in $STATIONSFILE"
exit 1
fi
echo "Playing ${STATION_NAME} at: ${STATION_URL}"
# clean up from before
rm -f $RADIOSOCKET
# if the radio mpv is not running, start it otherwise kill the existing mpv
PID=$(test -e $RADIOPID && cat $RADIOPID || echo -1)
if [ "$PID" -ne "-1" ]; then
kill -15 $PID || echo "radio was not playing"
fi
# start a new mpv instead playing the selected station
mpv --cache=yes --cache-secs=5 --demuxer-max-bytes=8192k --no-video --quiet --input-ipc-server=$RADIOSOCKET "$STATION_URL" >> $HOME/.radio/$(date --rfc-3339=date).log &
echo $! > $RADIOPID
notify-send "Playing: $STATION_NAME"
}
function rsend_cmd {
echo "{\"command\": [$1]}" | socat - $RADIOSOCKET
}
function rinfo {
title="$(rsend_cmd '"get_property", "metadata/by-key/icy-title"' | jq .data)"
echo $title | sed s/\"//g | xclip -sel clipboard
notify-send "Currently playing: $title"
echo $title
}
function ryt {
# search currently playing song on youtube with $BROWSER
title="$(rsend_cmd '"get_property", "metadata/by-key/icy-title"' | jq .data | sed 's/"//g')"
test -z "$title" && echo "No title found" && notify-send "Radio: No title found" && exit 1
$BROWSER "https://www.youtube.com/results?search_query=$title" &
echo "Looking up '$title' on youtube"
notify-send "Radio: Looking up '$title' on youtube"
}
function rmute {
# be careful with mute, for some reason some streams stop playing if they are muted for a long time
rsend_cmd '"set_property", "mute", true' > /dev/null && notify-send "Radio muted"
}
function runmute {
rsend_cmd '"set_property", "mute", false' > /dev/null && notify-send "Radio unmuted"
}
function rtoggle {
current_status=$(rsend_cmd '"get_property", "mute"' | jq .data)
if [[ $current_status == "true" ]];
then
runmute
else
rmute
fi
}
function rstop {
test -e $RADIOPID && kill -15 $(cat $RADIOPID) && notify-send "Radio stopped" || notify "Radio is not on"
}
function radd {
if (( $# != 2 ))
then
echo "Usage: radio add <station name> <station url>"
exit 1
else
printf "\"%s\",%s\n" "$1" "$2" >> $STATIONSFILE
fi
}
function rstations {
cat $STATIONSFILE | awk -F ',' '{printf("%-20s\t%s\n",$1,$2)}'
}
function rquiet {
rsend_cmd '"set_property", "volume", 40' > /dev/null && notify-send "Radio: quiet mode"
}
function rloud {
rsend_cmd '"set_property", "volume", 100' > /dev/null && notify-send "Radio: loud mode"
}
function rtoggle-volume {
current_volume=$(rsend_cmd '"get_property", "volume"' | jq .data)
if [[ $current_volume == "100" ]];
then
rquiet
else
rloud
fi
}
case "$1" in
start)
rstart ;;
stop)
rstop ;;
info)
rinfo ;;
yt)
ryt ;;
toggle)
rtoggle ;;
add)
radd "$2" "$3";;
stations)
rstations ;;
quiet)
rquiet ;;
loud)
rloud ;;
toggle-volume)
rtoggle-volume ;;
*)
cat <<EOF
Usage: radio [command]
List of commands:
- start: shows a list of stations and plays the one you choose
- stop: stops the radio (kills mpv)
- info: shows currently playing song (using notifications)
- toggle: mutes or unmutes the radio
- add: adds a radio station i.e. radio add <station name> <station url>
- stations: lists all radio stations in $STATIONSFILE
EOF
exit 1 ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment