-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmediactl.py
109 lines (97 loc) · 4.34 KB
/
mediactl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
# The following are basic functions for controlling available media players on a Linux system, using dbus.
# Intended for use with only one media player running, tho works with multiple just without separate controls.
# If dbus error, try setting include-system-site-packages = true in virtual environment's pyvenv.cfg file.
# by Nik Stromberg - [email protected] - MIT 2022 - copilot
from dbus import SessionBus, Interface
bus = SessionBus()
def _playerlist() -> list:
"""Returns a list of all available media player services, for mediactl functions."""
return [service for service in bus.list_names() if service.startswith('org.mpris.MediaPlayer2.')]
def playpause() -> int:
"""Toggles play/pause for all available media players, returns number successed."""
players = _playerlist()
worked = len(players)
for player in players:
try:
player = bus.get_object(player, '/org/mpris/MediaPlayer2')
player.PlayPause(dbus_interface='org.mpris.MediaPlayer2.Player')
except:
worked -= 1
return worked
def next() -> int:
"""Go to next track for all available media players, returns number successed."""
players = _playerlist()
worked = len(players)
for player in players:
try:
player = bus.get_object(player, '/org/mpris/MediaPlayer2')
player.Next(dbus_interface='org.mpris.MediaPlayer2.Player')
except:
worked -= 1
return worked
def prev() -> int:
"""Go to previous track for all available media players, returns number successed."""
players = _playerlist()
worked = len(players)
for player in players:
try:
player = bus.get_object(player, '/org/mpris/MediaPlayer2')
player.Previous(dbus_interface='org.mpris.MediaPlayer2.Player')
except:
worked -= 1
return worked
def stop() -> int:
"""Stop playback for all available media players, returns number successed."""
players = _playerlist()
worked = len(players)
for player in players:
try:
player = bus.get_object(player, '/org/mpris/MediaPlayer2')
player.Stop(dbus_interface='org.mpris.MediaPlayer2.Player')
except:
worked -= 1
return worked
def volumeup() -> int:
"""Increase volume for all available media players, returns number successed."""
players = _playerlist()
worked = len(players)
for player in players:
try:
player = bus.get_object(player, '/org/mpris/MediaPlayer2')
properties = Interface(player, dbus_interface='org.freedesktop.DBus.Properties')
volume = properties.Get('org.mpris.MediaPlayer2.Player', 'Volume')
properties.Set('org.mpris.MediaPlayer2.Player', 'Volume', volume+0.2)
except:
worked -= 1
return worked
def volumedown() -> int:
"""Decrease volume for all available media players, returns number successed."""
players = _playerlist()
worked = len(players)
for player in players:
try:
player = bus.get_object(player, '/org/mpris/MediaPlayer2')
properties = Interface(player, dbus_interface='org.freedesktop.DBus.Properties')
volume = properties.Get('org.mpris.MediaPlayer2.Player', 'Volume')
properties.Set('org.mpris.MediaPlayer2.Player', 'Volume', volume-0.2)
except:
worked -= 1
return worked
def status() -> list:
"""Returns list of dicts containing title, artist, & status for each media player."""
players = _playerlist()
details = []
for player in players:
try:
player = bus.get_object(player, '/org/mpris/MediaPlayer2')
properties = Interface(player, dbus_interface='org.freedesktop.DBus.Properties')
metadata = properties.Get('org.mpris.MediaPlayer2.Player', 'Metadata')
Title = metadata['xesam:title'] if 'xesam:title' in metadata else 'Unknown'
Artist = metadata['xesam:artist'][0] if 'xesam:artist' in metadata else 'Unknown'
PlayStatus = properties.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus')
details.append({'status': str(PlayStatus), 'title': str(Title), 'artist': str(Artist)})
except:
pass
return details
#if __name__ == '__main__': # If I decide to make this a standalone media controller later.