-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathswap-audio-output-device.py
More file actions
executable file
·44 lines (37 loc) · 1.36 KB
/
swap-audio-output-device.py
File metadata and controls
executable file
·44 lines (37 loc) · 1.36 KB
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
#!/usr/bin/env python3
import subprocess
import json
def main():
sinks = [
s
for s in pactl("list", "sinks")
if any((p.get("availability") != "not available" for p in s.get("ports", [])))
]
current_sink_name = pactl("info")["default_sink_name"]
print(f"Current sink name: {current_sink_name}")
current_sink = next(x for x in sinks if x.get("name") == current_sink_name)
print(f"Current sink: {current_sink}")
if not current_sink:
raise ValueError("Could not find defaut sink in sinks")
default_sink_index = sinks.index(current_sink)
next_sink = sinks[(default_sink_index + 1) % len(sinks)]
print(f"Next sink: {next_sink}")
sink_inputs = pactl("list", "sink-inputs")
for sink_input in sink_inputs:
print(f"Moving {sink_input['index']} to {next_sink['name']}")
pactl("move-sink-input", sink_input["index"], next_sink["index"])
pactl("set-default-sink", next_sink["index"])
def pactl(*args: str):
"""
Run pactl with the given arguments.
Sets the format to json so output is some parsed json.
"""
cmd = ["pactl", "-f", "json"] + list(args)
cmd = list(map(str, cmd))
res = subprocess.run(cmd, check=True, text=True, capture_output=True)
if res.stdout:
return json.loads(res.stdout)
else:
return None
if __name__ == "__main__":
main()