Skip to content

Commit

Permalink
Merge pull request #5 from smarkoco/main
Browse files Browse the repository at this point in the history
bug fix when having multiple audio sinks to not confuse processes
  • Loading branch information
nisiddharth authored Oct 15, 2024
2 parents fcd1947 + 911dceb commit b2adcf5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 54 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ This is purely a bash script.

Uses dbus, applicable for systems using PulseAudio. Only works while using Spotify client (doesn't matter how you install it) and not web browser.

Mutes and unmutes Spotify within 5 seconds of start and end of Ads.
Mutes and unmutes Spotify within 1 second of start and end of Ads.

### Usage:

To use just run `mute_spotify_timer.sh` either using terminal or Alt + F2 launcher or just add it to list of system Startup Applications. To allow monitoring the ongoing status, run `adfree.sh` as a Startup Application, or with `bash adfree.sh`. It creates a new background tmux session called `adfree`, which can be connected to with `tmux a -t adfree`.

In adfree.sh, you may specify respawn=true

### Maintenance:

I'll make this more efficient when I'll feel free. Meanwhile you can send Pull Requests!
2 changes: 1 addition & 1 deletion adfree.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
tmux new-session -d -s adfree
tmux send-keys '~/Spotify-Ads-Muter-Linux/mute_spotify_timer.sh' C-m # C-m sends ENTER keystroke
tmux send-keys "~/Spotify-Ads-Muter-Linux/mute_spotify_timer.sh" C-m
83 changes: 33 additions & 50 deletions mute_app.sh
Original file line number Diff line number Diff line change
@@ -1,70 +1,53 @@
#!/bin/bash
# Script to mute an application using PulseAudio, depending solely on
# process name, constructed as answer on askubuntu.com:.
# http://askubuntu.com/questions/180612/script-to-mute-an-application

# It works as: mute_application.sh vlc mute OR mute_application.sh vlc unmute
# Script to respawn or mute an application using PulseAudio

# it works as mute_app.sh <app_name> <action>, such as mute_app.sh spotify mute
if [ -z "$1" ]; then
echo "Please provide me with an application name"
echo "Please provide an application name."
exit 1
fi

if [ -z "$2" ]; then
echo "Please provide me with an action mute/unmute after the application name"
echo "Please provide an action: mute, unmute, or respawn."
exit 1
fi

if ! [[ "$2" == "mute" || "$2" == "unmute" ]]; then
echo "The 2nd argument must be mute/unmute"
if [[ "$2" != "mute" && "$2" != "unmute" && "$2" != "respawn" ]]; then
echo "The action must be mute, unmute, or respawn."
exit 1
fi

process_id=$(pidof "$1")
# Get the sink input index for the sink with matching application name, and use grep to find all matching indexes
sink_input_indexes=$(pacmd list-sink-inputs | grep -B 20 "application.name = \"$1\"" | awk '/index:/{print $2}')
# this second command catches Chromium commercials
sink_input_indexes_alt=$(pacmd list-sink-inputs | grep -B 27 "application.process.binary = \"$1\"" | awk '/index:/{print $2}')
# Now combine the sink input indexes from both commands to remove duplicates
sink_input_indexes=$(echo -e "$sink_input_indexes\n$sink_input_indexes_alt" | sort -n | uniq)

if [ $? -ne 0 ]; then
echo "There is no such process as "$1""
if [ -z "$sink_input_indexes" ]; then
echo "Could not find sink input index for $1."
exit 1
fi

temp=$(mktemp)

pacmd list-sink-inputs > $temp

inputs_found=0;
current_index=-1;

while read line; do
if [ $inputs_found -eq 0 ]; then
inputs=$(echo -ne "$line" | awk '{print $2}')
if [[ "$inputs" == "to" ]]; then
continue
fi
inputs_found=1
else
if [[ "${line:0:6}" == "index:" ]]; then
current_index="${line:7}"
elif [[ "${line:0:25}" == "application.process.id = " ]]; then
if [[ "${line:25}" == "\"$process_id\"" ]]; then
# index found...
break;
fi
fi
# Perform the specified action on each sink input index
for sink_input_index in $sink_input_indexes; do
if [ "$2" == "mute" ]; then
pacmd set-sink-input-mute "$sink_input_index" 1 > /dev/null 2>&1
echo "Muted $1 on sink input index $sink_input_index."
elif [ "$2" == "unmute" ]; then
pacmd set-sink-input-mute "$sink_input_index" 0 > /dev/null 2>&1
echo "Unmuted $1 on sink input index $sink_input_index."
elif [ "$2" == "respawn" ]; then
# Kill the application
pkill -x "$1"
# Wait for the application to close
while pgrep -x "$1" > /dev/null; do sleep 1; done
# Restart the application
"$1" > /dev/null 2>&1 &
echo "Respawned $1."
# break out of the loop after respawning the application because this is only needed once
break
fi
done < $temp

rm -f $temp

if [ $current_index -eq -1 ]; then
echo "Could not find "$1" in the processes that output sound."
exit 1
fi

# muting...
if [[ "$2" == "mute" ]]; then
pacmd set-sink-input-mute "$current_index" 1 > /dev/null 2>&1
else
pacmd set-sink-input-mute "$current_index" 0 > /dev/null 2>&1
fi
done

exit 0
10 changes: 8 additions & 2 deletions mute_spotify_timer.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#!/bin/bash

respawn=false # set whether to mute or restart spotify to skip ads

while true; do
# get Spotify playing song name
name=`dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata | sed -n '/title/{n;p}' | cut -d '"' -f 2`
url=`dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata | sed -n '/url/{n;p}' | cut -d '"' -f 2`
echo $name

if [[ "$name" = *"Advertisement"* || "$name" = *"Spotify"* ]] || [[ $url == *"/ad/"* ]]; then
# if song name contains "Advertisement" or "Spotify" or url contains "/ad/"
if [[ "$name" = *"Advertisement"* || "$name" = *"Spotify"* ]] || [[ $url == *"/ad/"* ]]; then
echo "Muting"
~/Spotify-Ads-Muter-Linux/mute_app.sh spotify mute
if [[ $respawn == true ]]; then
echo "Respawning"
~/Spotify-Ads-Muter-Linux/mute_app.sh spotify respawn
fi
else
echo "Unmuting"
~/Spotify-Ads-Muter-Linux/mute_app.sh spotify unmute
Expand Down

0 comments on commit b2adcf5

Please sign in to comment.