spotify-adblocker-for-linux
#!/bin/bash
#SOURCE:https://gist.github.com/pcworld/3198763
# spotify-automute-simple
# - automatically mute Spotify when ad comes on and loop short track
# - automatically switch back to Spotify when ad over
# Settings
PLAYER="mpv --loop=inf"
LOOPTRACK="$HOME/.spotify-ad-track/spotify-ad-track.mp3"
# VAR
WMTITLE="Spotify Hacked for Linux "
ADMUTE=0
PAUSED=0
# FCT
get_pactl_nr(){
pactl list | grep -E '(^Sink Input)|(media.name = \"Spotify\"$)' | cut -d \# -f2 \
| grep -v Spotify
}
# MAIN
xprop -spy -name "$WMTITLE" WM_ICON_NAME |
while read -r XPROPOUTPUT; do
XPROP_TRACKDATA="$(echo "$XPROPOUTPUT" | cut -d \" -f 2 )"
DBUS_TRACKDATA="$(dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify / \
org.freedesktop.MediaPlayer2.GetMetadata | grep xesam:title -A 1 | grep variant | cut -d \" -f 2)"
echo "XPROP: $XPROP_TRACKDATA"
echo "DBUS: $DBUS_TRACKDATA"
if [[ "$XPROP_TRACKDATA" = "Spotify" ]]
then
echo "--PAUSED--"
PAUSED="1"
else
PAUSED=0
echo "--NOTPAUSED--"
fi
if [[ "$PAUSED" = "1" || "$XPROP_TRACKDATA" =~ "$DBUS_TRACKDATA" ]]
then
echo "--NOAD--"
if [[ "$ADMUTE" = "1" ]]
then
sleep 0.5
kill -s TERM "$ALTPID"
for PACTLNR in $(get_pactl_nr); do
pactl set-sink-input-mute "$PACTLNR" no > /dev/null 2>&1
echo "Unmuting sink $PACTLNR"
done
fi
ADMUTE=0
else
echo "--AD--"
if [[ "$ADMUTE" != "1" ]]
then
for PACTLNR in $(get_pactl_nr); do
pactl set-sink-input-mute "$PACTLNR" yes > /dev/null 2>&1
echo "Muting sink $PACTLNR"
done
$PLAYER "$LOOPTRACK" > /dev/null 2>&1 &
ALTPID="$!"
fi
ADMUTE=1
fi
done
echo "Spotify not active. Exiting."
exit 0