Skip to content

Commit

Permalink
bug fix when having multiple audio sinks to not confuse processes
Browse files Browse the repository at this point in the history
  • Loading branch information
smarkoco committed Feb 27, 2024
1 parent fcd1947 commit a1e2ced
Show file tree
Hide file tree
Showing 4 changed files with 42 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 "~/git/Spotify-Ads-Muter-Linux/mute_spotify_timer.sh $respawn" C-m
76 changes: 28 additions & 48 deletions mute_app.sh
Original file line number Diff line number Diff line change
@@ -1,70 +1,50 @@
#!/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 process ID associated with the specified application
process_ids=$(pgrep -d ' ' -x "$1")

if [ $? -ne 0 ]; then
echo "There is no such process as "$1""
if [ -z "$process_ids" ]; then
echo "There is no such process matching: '$1'."
exit 1
fi

temp=$(mktemp)

pacmd list-sink-inputs > $temp

inputs_found=0;
current_index=-1;
# Loop through each process ID
for process_id in $process_ids; do
# Get the sink input index for the sink with "spotify" in the name
sink_input_index=$(pacmd list-sink-inputs | grep -B 20 -P "application.name = \"${1}\"" | grep "index" | awk '{print $2}')

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
if [ -z "$sink_input_index" ]; then
echo "Could not find sink input index for '$1' (PID: $process_id)."
exit 1
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
# Perform the specified action
if [ "$2" == "mute" ]; then
pacmd set-sink-input-mute "$sink_input_index" 1 > /dev/null 2>&1
elif [ "$2" == "unmute" ]; then
pacmd set-sink-input-mute "$sink_input_index" 0 > /dev/null 2>&1
elif [ "$2" == "respawn" ]; then
kill "$process_id" # kill application
while pgrep -x "$1" >/dev/null; do sleep 1; done # wait for application to die
"$1" & # start application
fi
done

exit 0
14 changes: 10 additions & 4 deletions mute_spotify_timer.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#!/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
~/git/Spotify-Ads-Muter-Linux/mute_app.sh spotify mute
if [[ $respawn == true ]]; then
echo "Respawning"
~/git/Spotify-Ads-Muter-Linux/mute_app.sh spotify respawn
fi
else
echo "Unmuting"
~/Spotify-Ads-Muter-Linux/mute_app.sh spotify unmute
~/git/Spotify-Ads-Muter-Linux/mute_app.sh spotify unmute
fi
sleep 1
done

0 comments on commit a1e2ced

Please sign in to comment.