Skip to content

Instantly share code, notes, and snippets.

@logical-and
Forked from pcworld/_README.md
Last active October 22, 2023 13:02
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save logical-and/825bab160d604d82bf6ad9ebd3a6410d to your computer and use it in GitHub Desktop.
Save logical-and/825bab160d604d82bf6ad9ebd3a6410d to your computer and use it in GitHub Desktop.
Linux Spotify Ad Mute

We all love Spotify, but sometimes people (like us) want to throw a party without having to listen to interrupting ads before having bought Spotify Premium. Well, with this killer project, now you can!

This is for testing purposes ONLY! Spotify is a fantastic service and worth every penny. This script is NOT meant to circumvent buying premium! Please do consider switching to premium to support Spotify - especially if you're going to use it on mobile. If the script does not work for you, help us improve it!

Usage

wget -O spotify-mute-ads.sh https://gist.githubusercontent.com/logical-and/825bab160d604d82bf6ad9ebd3a6410d/raw/84f77518f6fa8980e73fcf1fadd30d223f2100a1/spotify-mute-ads.sh
chmod ug+x spotify-mute-ads.sh
./spotify-mute-ads.sh 
# - OR
nohup ./spotify-mute-ads.sh  > /tmp/spotify_mute.log

Dependencies

Utilities used in the script:

  • xprop
  • pacmd
  • xdotool

Credits

This script is based on:

Thank y'all guys!

#!/bin/bash
# Mute spotify when it starts advertisement
# Run like this
# nohup ./spotify-mute-ads.sh > /tmp/spotify_mute.log &
# or
# ./spotify-mute-ads.sh
# Link: https://gist.githubusercontent.com/logical-and/825bab160d604d82bf6ad9ebd3a6410d/raw/42d681bd8c6995c534c386718dd7914a6d5f0709/spotify-admute.sh
# Link: https://gist.github.com/pcworld/3198763/af2a4981c6f26075c0a16a261f5294f2cf5e5da5#gistcomment-3384837
# Settings
PAUSE_BEFORE_UNMUTE=2
PAUSE_NOT_ACTIVE=5
# VAR
AD_MUTED=0
AD_DETECTED=0
# FCT
get_pactl_nr() {
pactl list | grep -E '(^Sink Input)|(media.name = \"Spotify\"$)' | cut -d \# -f2 \
| grep -v Spotify
}
mute_player() {
num=$(pactl list | grep -E '(^Sink Input)|(media.name = \"Spotify\"$)' | awk '/Spotify/ {print a} {a = $0}' | cut -c 13- | tail -n 1)
pactl set-sink-input-mute $num yes
}
unmute_player() {
num=$(pactl list | grep -E '(^Sink Input)|(media.name = \"Spotify\"$)' | awk '/Spotify/ {print a} {a = $0}' | cut -c 13- | tail -n 1)
pactl set-sink-input-mute $num no
}
log() {
local line=${1}
echo \[$( date "+%H:%M:%S" )\] $line
}
# Unmute just in case this script crashed the previous time
unmute_player
# Endless loop, sort of service
while [ 1 ]; do
# Find window id
WM_ID=$(xdotool search --class "spotify" | sed -n 2p)
if [ -z "$WM_ID" ]; then
log "Spotify not active"
sleep $PAUSE_NOT_ACTIVE
continue
fi
# MAIN
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)"
if [[ "$DBUS_TRACKDATA" =~ ^(Advertisement|Spotify)$ ]]; then
log "AD detected"
AD_DETECTED="1"
else
AD_DETECTED="0"
log "No AD detected"
fi
# Debug (uncomment if you need to debug the script)
# echo "XPROP: \"$XPROP_TRACKDATA\""
# echo "DBUS: \"$DBUS_TRACKDATA\""
# echo "Debug: AD_MUTED = $AD_MUTED, AD_DETECTED = $AD_DETECTED"
if [[ "$AD_DETECTED" = "0" ]] && [[ "$AD_MUTED" = "1" ]]; then
log "Unmuting (AD_MUTED = $AD_MUTED, AD_DETECTED = $AD_DETECTED)"
sleep $PAUSE_BEFORE_UNMUTE
unmute_player
AD_MUTED="0"
log "Unmuted"
elif [[ "$AD_DETECTED" = "1" ]] && [[ "$AD_MUTED" = "0" ]]; then
log "Muting (AD_MUTED = $AD_MUTED, AD_DETECTED = $AD_DETECTED)"
mute_player
AD_MUTED="1"
log "Muted"
fi
done
log "Spotify is not active, unmuting"
unmute_player
AD_MUTED="1"
done
log "Something went wrong. Exiting."
exit 1
@ron-debajyoti
Copy link

Perhaps this requires an update as it isn't working for the latest Spotify update on Linux.

@logical-and
Copy link
Author

logical-and commented Apr 8, 2021

It works for me, what version of Spotify do you use? Mine is 1.1.55.498.gf9a83c60

@ron-debajyoti
Copy link

ron-debajyoti commented Apr 8, 2021

I can't find the version number. I'm using Ubuntu so could you tell me where to look at? Looking at running processes, I think its Spotify/1.1.55.498

In my case, the script just says Spotify Not Active

@ron-debajyoti
Copy link

Never mind I fixed it for my case. The issue was in Line 47. The sed command was not required for me.

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