Skip to content

Instantly share code, notes, and snippets.

@jeansch
Created May 16, 2014 19:49
Show Gist options
  • Save jeansch/e4a58ac7f36316f16fbe to your computer and use it in GitHub Desktop.
Save jeansch/e4a58ac7f36316f16fbe to your computer and use it in GitHub Desktop.
Change pulseaudio default output and switch currently playing programs to that output
#!/usr/bin/env python
import sys
import subprocess
default_index = None
current_sink = None
outputs = {}
sinks, _ = subprocess.Popen('pacmd list-sinks', shell=True,
stdout=subprocess.PIPE).communicate()
for line in sinks.split('\n'):
if line.startswith(' index'):
index = line.split(':')[1].strip()
current_index = index
if line.startswith(' * index'):
index = line.split(':')[1].strip()
default_index = index
current_index = index
if line.startswith("\tname:"):
name = line.split(':')[1].strip()
if current_index:
outputs[current_index] = name
to_move = set()
inputs, _ = subprocess.Popen('pacmd list-sink-inputs', shell=True,
stdout=subprocess.PIPE).communicate()
for line in inputs.split('\n'):
if line.startswith(' index'):
index = line.split(':')[1].strip()
to_move.add(index)
if len(sys.argv) > 1:
selected = sys.argv[1].lower()
if selected not in outputs.keys():
for index, output in outputs.items():
if selected in output.lower():
selected = index
break
subprocess.call(["pacmd", "set-default-sink", selected])
for input in to_move:
subprocess.call(["pacmd", "move-sink-input", input, selected])
else:
for index, output in outputs.items():
print "%s %s %s" % (index, output,
"SELECTED" if index == default_index else "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment