run a command based on an http request (system volume)

Discuss your Lua playlist, album art and interface scripts.
fiore42
New Cone
New Cone
Posts: 4
Joined: 06 Jul 2019 09:26

run a command based on an http request (system volume)

Postby fiore42 » 08 Feb 2025 02:59

I'd like to add a 2nd volume slider in mobile.html to control not just vlc volume but also the actual computer volume using amixer (on a Raspberry Pi).

First I managed to read the system volume with this edit to /usr/share/vlc/lua/http/requests/status.xml

Code: Select all

> -- Fetch current system volume > local function get_system_volume() > local handle = io.popen("amixer get Master | grep -o '[0-9]*%' | head -1 | tr -d '%'") > if handle then > local sys_volume = handle:read("*a") > handle:close() > return sys_volume:gsub("\n", "") or "50" -- Remove newlines, default to 50 > else > return "50" > end > end > 37a50,51 > > 38a53,54 > > print("<systemvolume>" .. get_system_volume() .. "</systemvolume>\n")
but I never managed to get status.xml to read something like pi.local:2222/requests/status.xml?command=system_volume&val=90

Then I moved to writing a lua extension, with the hope to send a command with something like http://pi.local:2222/set_volume?volume=15 but so far I only got 404.

Below is my current draft of /usr/share/vlc/lua/extensions/system_volume.lua - but the logs say

Code: Select all

Trying Lua playlist script /usr/share/vlc/lua/extensions/system_volume.lua
which doesn't give me a lot of confidence (why playlist? wrong folder?) And I also cannot see "[Volume Extension] Activated" in the logs.

Any tip or working sample I could use to better understand?

EDIT: I just noticed I did not check "Lua interpreter" under Extra interface modules, but it didn't make any difference.
Also, I'm not sure about the dropdown "Interface module". Should I pick Lua interpreter there?

Code: Select all

# cat system_volume.lua -- Define descriptor() FIRST, at the top level: function descriptor() return { title = "System Volume Control", version = "1.0", author = "Your Name", description = "Control system volume using amixer", capabilities = {} -- Return an EMPTY table for capabilities } end -- Then define the rest of your functions: local volume_extension = { set_system_volume = function(volume) local handle = io.popen("amixer set Master " .. volume .. "%") local result = handle:read("*a") handle:close() if result then vlc.msg.info("[Volume Extension] System volume set to " .. volume .. "%") return true else vlc.msg.err("[Volume Extension] Error setting system volume: " .. (result or "Unknown error")) return false end end, get_system_volume = function() local handle = io.popen("amixer get Master") local result = handle:read("*a") handle:close() if result then local volumeMatch = string.match(result, "Playback %d+ \\[([%d]+)%%\\]") if volumeMatch then return tonumber(volumeMatch) else vlc.msg.err("[Volume Extension] Could not parse volume from amixer output. Result: " .. result) -- Debugging return nil end else vlc.msg.err("[Volume Extension] Error getting volume from amixer.") return nil end end, httpHandlers = { set_volume = function(request) local volume = request.query_params.volume or request.form.volume if volume then volume = tonumber(volume) if volume and volume >= 0 and volume <= 100 then if volume_extension.set_system_volume(volume) then return { status = 200, body = "Volume set: " .. volume .. "%" } else return { status = 500, body = "Error setting volume" } end else return { status = 400, body = "Invalid volume value (0-100)" } end else return { status = 400, body = "Missing volume parameter" } end end, get_volume = function(request) local volume = volume_extension.get_system_volume() if volume then return { status = 200, body = tostring(volume) } else return { status = 500, body = "Error getting volume" } end end } } function activate() vlc.msg.info("[Volume Extension] Activated") -- Register HTTP handlers directly: vlc.http.add_handler("/set_volume", volume_extension.httpHandlers.set_volume) vlc.http.add_handler("/get_volume", volume_extension.httpHandlers.get_volume) end function deactivate() vlc.msg.info("[Volume Extension] Deactivated") vlc.http.remove_handler("/set_volume") vlc.http.remove_handler("/get_volume") end function close() vlc.deactivate() end

fiore42
New Cone
New Cone
Posts: 4
Joined: 06 Jul 2019 09:26

Re: run a command based on an http request (system volume)

Postby fiore42 » 08 Feb 2025 23:41

If anyone has the same need, I built a super basic version of it with flask

https://github.com/fiore42/extended_VLC_remote

Image


Return to “Scripting VLC in lua”

Who is online

Users browsing this forum: No registered users and 34 guests