Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Last active October 16, 2025 13:51
Show Gist options
  • Select an option

  • Save ZiTAL/8b1895dc57069f6cb66357b75814624d to your computer and use it in GitHub Desktop.

Select an option

Save ZiTAL/8b1895dc57069f6cb66357b75814624d to your computer and use it in GitHub Desktop.
python: VLC increase / decrease volume
import sys
import re
import time
import subprocess
timeout = 5
app_name = r"^VLC\s+media\s+player"
def getApp(name):
command = "pactl list sink-inputs | grep -E \"Sink Input|application.name|media.name|Volume\""
result = subprocess.getoutput(f"{command}")
m = re.findall(r"Sink Input #([0-9]+)\s*\n\s+Volume:\s+[^\s]+\s+[0-9]+\s+\/\s+([0-9]+)%[^\n]+\n\s+application\.name\s+=\s+\"([^\n]+)\"", result, re.MULTILINE)
apps = []
for i in m:
app = {
'id': i[0],
'name': i[2],
'volume': int(i[1])
}
apps.append(app)
app = next((item for item in apps if re.search(rf"{app_name}", item['name'])), None)
return app
def decreaseVolumeToApp(app_name, timeout):
app = getApp(app_name)
if app != None and app['volume'] > 0:
delay = timeout / app['volume']
for i in range(app['volume'], -1, -1):
command = f"pactl set-sink-input-volume {app['id']} {i}%"
subprocess.getoutput(f"{command}")
print(f"{i}")
time.sleep(delay)
return True
return False
for i in range(0, 5):
if decreaseVolumeToApp(app_name, timeout) == True:
sys.exit(0)
print(i)
time.sleep(1)
sys.exit(1)
import sys
import subprocess
import re
import time
volume = 85
app_name = r"^VLC\s+media\s+player"
def getApp(name):
command = "pactl list sink-inputs | grep -E \"Sink Input|application.name|media.name|Volume\""
result = subprocess.getoutput(f"{command}")
m = re.findall(r"Sink Input #([0-9]+)\s*\n\s+Volume:\s+[^\s]+\s+[0-9]+\s+\/\s+([0-9]+)%[^\n]+\n\s+application\.name\s+=\s+\"([^\n]+)\"", result, re.MULTILINE)
apps = []
for i in m:
app = {
'id': i[0],
'name': i[2],
'volume': int(i[1])
}
apps.append(app)
app = next((item for item in apps if re.search(rf"{app_name}", item['name'])), None)
return app
def increaseVolumeToApp(app_name, volume):
app = getApp(app_name)
if app != None:
command = f"pactl set-sink-input-volume {app['id']} {volume}%"
subprocess.getoutput(f"{command}")
return True
return False
for i in range(0, 5):
if increaseVolumeToApp(app_name, volume) == True:
sys.exit(0)
print(i)
time.sleep(1)
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment