Skip to content

Instantly share code, notes, and snippets.

@angeloped
Created March 31, 2019 16:35
Show Gist options
  • Save angeloped/2ffc421aa269ff298b602030efc38f0f to your computer and use it in GitHub Desktop.
Save angeloped/2ffc421aa269ff298b602030efc38f0f to your computer and use it in GitHub Desktop.
Change master audio volume in Linux using python.
import subprocess
def get_master_volume():
proc = subprocess.Popen('/usr/bin/amixer sget Master', shell=True, stdout=subprocess.PIPE)
amixer_stdout = proc.communicate()[0].split('\n')[4]
proc.wait()
find_start = amixer_stdout.find('[') + 1
find_end = amixer_stdout.find('%]', find_start)
return float(amixer_stdout[find_start:find_end])
def set_master_volume(volume):
val = volume
val = float(int(val))
proc = subprocess.Popen('/usr/bin/amixer sset Master ' + str(val) + '%', shell=True, stdout=subprocess.PIPE)
proc.wait()
print("Current volume: ", get_master_volume())
set_master_volume(0)
print("Current volume (changed): ", get_master_volume())
set_master_volume(50)
print("Current volume (changed): ", get_master_volume())
@edib
Copy link

edib commented Jul 13, 2021

In ubuntu 20.04 only following works:
amixer_stdout = str(proc.communicate()[0],'UTF-8').split('\n')[4]

@naseemap47
Copy link

Nice Work ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment