Project

General

Profile

Spotify + audacious keyboard shortcuts sharing

Added by Marcin Kocur about 1 year ago

So I'm using audacious and Spotify. I wanted to have exactly the same keyboard shortcuts for controlling both. I prepared few scripts for that and assigned them to keys.

Lower volume:

#!/usr/bin/env python3
#Author: Marcin Kocur, attribution license: https://creativecommons.org/licenses/by/4.0/
#Spotify part

import subprocess
import os

x=0
y=0
env = os.environ
env['LANG'] = 'en_US'
app = '"Spotify"'

pactl = subprocess.check_output(['pactl', 'list', 'sink-inputs'], env=env).decode().strip().split()

if app in pactl:
    for e in pactl:
        x += 1
        if e == app:
            break
    for i in pactl[0 : x -1 ]:
        y += 1
        if i == 'Sink' and pactl[y] == 'Input' and '#' in pactl[y + 1]:
            sink_id = pactl[y+1]
        if i == 'Volume:' and '%' in pactl[y + 3]:
            volume = pactl[y + 3]
    sink_id = sink_id[1: ]
    volume = volume[ : -1 ]

    if not int(volume) <= 0:
        subprocess.run(['pactl', 'set-sink-input-volume', sink_id, '-1%'])

# Audacious part
volume = subprocess.check_output( ['audtool', 'get-volume'] )
if not int(volume) <= 0:
    subprocess.run(['audtool', 'set-volume', str(int(volume) - 1)] )

Volume up:

#!/usr/bin/env python3

#Spotify part

import subprocess
import os

x=0
y=0
env = os.environ
env['LANG'] = 'en_US'
app = '"Spotify"'

pactl = subprocess.check_output(['pactl', 'list', 'sink-inputs'], env=env).decode().strip().split()

if app in pactl:
    for e in pactl:
        x += 1
        if e == app:
            break
    for i in pactl[0 : x -1 ]:
        y += 1
        if i == 'Sink' and pactl[y] == 'Input' and '#' in pactl[y + 1]:
            sink_id = pactl[y+1]
        if i == 'Volume:' and '%' in pactl[y + 3]:
            volume = pactl[y + 3]
    sink_id = sink_id[1: ]
    volume = volume[ : -1 ]

    if int(volume) < 100:
        subprocess.run(['pactl', 'set-sink-input-volume', sink_id, '+1%'])

# Audacious part
volume = subprocess.check_output( ['audtool', 'get-volume'] )
if int(volume) < 100:
    subprocess.run(['audtool', 'set-volume', str(int(volume) + 1)] )

Advance song:

#!/usr/bin/bash

#Spotify
if [ `qdbus org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlaybackStatus` = 'Playing' ]; then
    dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next
fi
# Audacious
if [ `audtool playback-status` = 'playing' ]; then
    audtool playlist-advance
fi

Previous song:

#!/usr/bin/bash

#Spotify
if [ `qdbus org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlaybackStatus` = 'Playing' ]; then
    dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous
    dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous
fi
# Audacious
if [ `audtool playback-status` = 'playing' ]; then
    audtool playlist-reverse
fi

Play/Pause:

#!/usr/bin/bash
#Author: Marcin Kocur, attribution license: https://creativecommons.org/licenses/by/4.0/

#Spotify
spotify=`qdbus org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlaybackStatus`
audacious=`audtool playback-status`

function SpotifyPlayPause
{
        dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
    }

function AudaciousPlayPause
{
    audtool playback-playpause
    }

#Both Players making noise
if [ $spotify = 'Playing' ] && [ $audacious = 'playing' ]; then
        AudaciousPlayPause
        SpotifyPlayPause
        echo 'SpotifyAudacious' > "$(dirname "$0")/player_resume" 
        exit
fi

#Only Audacious is playing:
if [ "$audacious" = 'playing' ] && [ $spotify != 'Playing' ]; then
        AudaciousPlayPause
        echo 'Audacious' > "$(dirname "$0")/player_resume" 
        exit
fi

#Only Spotify is playing:
if [ "$audacious" != 'playing' ] && [ $spotify = 'Playing' ]; then
        SpotifyPlayPause
        echo 'Spotify' > "$(dirname "$0")/player_resume" 
        exit
fi

#Both players are quiet: ###########################
if [ `cat "$(dirname "$0")/player_resume"` = 'Audacious' ]; then # but Audacious played before
    AudaciousPlayPause
    rm "$(dirname "$0")/player_resume" 
    exit
fi

if [ `cat "$(dirname "$0")/player_resume"` = 'Spotify' ]; then # but Spotify played before
    SpotifyPlayPause
    rm "$(dirname "$0")/player_resume" 
    exit
fi

if [ `cat "$(dirname "$0")/player_resume"` = 'SpotifyAudacious' ]; then # but both played before
    SpotifyPlayPause
    AudaciousPlayPause
    rm "$(dirname "$0")/player_resume" 
    exit
fi

if [ `pidof spotify` ]; then # but none played before, we assume that if spotify is running, then we want it to play
    SpotifyPlayPause
    exit
fi

if [ `pidof audacious` ]; then # if spotify is not running, we shall try Audacious
    AudaciousPlayPause
    exit
fi