Created
March 31, 2019 16:35
-
-
Save angeloped/2ffc421aa269ff298b602030efc38f0f to your computer and use it in GitHub Desktop.
Change master audio volume in Linux using python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In ubuntu 20.04 only following works:
amixer_stdout = str(proc.communicate()[0],'UTF-8').split('\n')[4]