Skip to content

Instantly share code, notes, and snippets.

@ibLeDy
Forked from pcworld/_README.md
Last active December 20, 2023 11:23
Show Gist options
  • Save ibLeDy/7f99756ef7c70a9edd4118f38b83f23e to your computer and use it in GitHub Desktop.
Save ibLeDy/7f99756ef7c70a9edd4118f38b83f23e to your computer and use it in GitHub Desktop.
Linux Spotify Ad Mute
#!/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
@ibLeDy
Copy link
Author

ibLeDy commented Jun 4, 2020

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