Created
May 23, 2014 15:14
-
-
Save rhoconlinux/906b2d3a70bc96f066a6 to your computer and use it in GitHub Desktop.
spotify-adblocker-for-linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment