Skip to content

Instantly share code, notes, and snippets.

@salahmak
Last active May 27, 2021 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salahmak/56640000886e8c9d4ae3aeba21511909 to your computer and use it in GitHub Desktop.
Save salahmak/56640000886e8c9d4ae3aeba21511909 to your computer and use it in GitHub Desktop.
A bash script to mute spotify ads until they are done playing, we all hate them annoying ads...
#!/bin/bash
# NOTICE: THE "METADATA" FUNCTION BELOW HAS BEEN COPIED FROM "SP" GIST
# PLEASE CONSIDER CHECKING THE ORIGINAL CODE HERE: https://gist.github.com/wandernauta/6800547
#this represents the delay to run the loop (better be higher than 0.5 if
#you have a bad cpu
interval=1
# Functions
function checkDependency {
if command -v $1 >/dev/null 2>&1 ; then
printf ""
else
echo "$1 not found, please install it and then execute the script again"
echo "and then execute the script again. Thanks"
exit 1
fi
}
function SetVolume {
sink_input_id=$(pactl list sink-inputs | grep -B 17 "Spotify" | grep "Sink Input" | cut -c13-)
pactl set-sink-input-volume "${sink_input_id}" "${1}"%
}
function metadata {
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 -Ev "^method" `# Ignore the first line.` \
| grep -Eo '("(.*)")|(\b[0-9][a-zA-Z0-9.]*\b)' `# Filter interesting fiels.`\
| sed -E '2~2 a|' `# Mark odd fields.` \
| tr -d '\n' `# Remove all newlines.` \
| sed -E 's/\|/\n/g' `# Restore newlines.` \
| sed -E 's/(xesam:)|(mpris:)//' `# Remove ns prefixes.` \
| sed -E 's/^"//' `# Strip leading...` \
| sed -E 's/"$//' `# ...and trailing quotes.` \
| sed -E 's/"+/|/' `# Regard "" as seperator.` \
| sed -E 's/ +/ /g' `# Merge consecutive spaces.`
}
#############################################################################################
muted=false
previousTitle=""
currentTitle=""
checkDependency playerctl
while true; do
status=`pidof spotify | wc -l`
if [[ "$status" != 1 ]]; then
echo "Please Start spotify for the script to work"
exit 1
else
artist=$(metadata | grep --color=never -E "(artist)" | sed 's/artist|//')
currentTitle=$(metadata | grep --color=never -E "(title)" | sed 's/title|//')
if [ "$currentTitle" != "$previousTitle" ] && [ "$currentTitle" != "Advertisement" ]; then
echo "Now playing: ${artist} ${currentTitle}"
previousTitle="$currentTitle"
fi
[ "$currentTitle" = "Advertisement" ] && isPlayingAd=true || isPlayingAd=false
if [ "$muted" = false ] && [ "$isPlayingAd" = true ]; then
SetVolume 0
muted=true
echo "muted ad"
elif [ "$muted" = true ] && [ "$isPlayingAd" = false ]; then
sleep 1
SetVolume 100
muted=false
fi
sleep "$interval"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment