-
-
Save ibLeDy/7f99756ef7c70a9edd4118f38b83f23e to your computer and use it in GitHub Desktop.
Linux Spotify Ad Mute
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
#!/usr/bin/env bash | |
ADMUTE=0 | |
PAUSED=0 | |
get_pactl_nr(){ | |
pactl list | grep -E '(^Sink Input)|(media.name = \"Spotify\"$)' | cut -d \# -f2 \ | |
| grep -v Spotify | |
} | |
# Find window id | |
WM_ID=$(xdotool search --class "spotify" | sed -n 2p) | |
xprop -spy -id "$WM_ID" WM_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/mpris/MediaPlayer2 \ | |
org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata' | grep xesam:title -A 1 | grep variant | cut -d \" -f 2)" | |
echo "XPROP: $XPROP_TRACKDATA" | |
echo "DBUS: $DBUS_TRACKDATA" | |
if [[ "$DBUS_TRACKDATA" = "Advertisement" ]] | |
then | |
echo "--PAUSED--" | |
PAUSED="1" | |
else | |
PAUSED=0 | |
echo "--NOTPAUSED--" | |
fi | |
if [[ "$PAUSED" = 0 ]] | |
then | |
# || "$XPROP_TRACKDATA" =~ "$DBUS_TRACKDATA" ]] | |
echo "--NOAD--" | |
echo "ADMUTE: $ADMUTE" | |
echo "PAUSED: $PAUSED" | |
if [[ "$ADMUTE" = "1" ]] | |
then | |
sleep 0.5 | |
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--" | |
echo "ADMUTE: $ADMUTE" | |
echo "PAUSED: $PAUSED" | |
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 | |
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
Based on https://gist.github.com/pcworld/3198763#gistcomment-2950338