-
Notifications
You must be signed in to change notification settings - Fork 9
/
pa_mute.py
52 lines (46 loc) · 1.29 KB
/
pa_mute.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
#!/usr/bin/python3
import sys
from subprocess import Popen, STDOUT, PIPE
args = {}
positionals = []
for arg in sys.argv[1:]:
if '--' == arg[:2]:
if '=' in arg:
key, val = [x.strip() for x in arg[2:].split('=')]
else:
key, val = arg[2:], True
args[key] = val
else:
positionals.append(arg)
def run(cmd, *args, **kwargs):
handle = Popen(cmd, shell='True', stdout=PIPE, stderr=STDOUT, **kwargs)
output = b''
while handle.poll() is None:
data = handle.stdout.read()
if len(data):
output += data
data = handle.stdout.read()
output += data
handle.stdout.close()
return output
o = run('/usr/bin/pacmd list-sink-inputs')
index = 0
state = 'no'
apps = {}
for line in o.split(b'\n'):
if b'index' in line:
index = int(line.split(b':',1)[1])
if b'application.name' in line:
if not index in apps: apps[index] = {}
apps[index]['name'] = line.split(b'=',1)[1].strip(b'" \\.;-').decode('UTF-8').lower()
if b'muted' in line:
if not index in apps: apps[index] = {}
apps[index]['muted'] = line.split(b':',1)[1].strip().decode('UTF-8').lower()
if 'mute' in args:
for index in apps:
if apps[index]['name'] == args['mute']:
if apps[index]['muted'] == 'yes':
run(f'/usr/bin/pacmd set-sink-input-mute {index} 0')
else:
run(f'/usr/bin/pacmd set-sink-input-mute {index} 1')
print(apps)