A stripped console-only Lua CLI for use with shell scripts

Discuss your Lua playlist, album art and interface scripts.
nokangaroo
Blank Cone
Blank Cone
Posts: 29
Joined: 30 Dec 2010 17:11

A stripped console-only Lua CLI for use with shell scripts

Postby nokangaroo » 05 Mar 2015 06:22

Consider this to be part of the VLC documentation (which is extremely lacking).
Feedback would be welcome. The copyright notice is adapted from host.lua.

Code: Select all

--[==========================================================================[ shell.lua: A stripped console-only CLI for use with shell scripts --[==========================================================================[ Copyright (C) 2015 nokangaroo nokangaroo@NOSPAM.aon.at Work in progress Based on host.lua and cli.lua host.lua author: Antoine Cellerier <dionoea at videolan dot org> cli.lua authors: Antoine Cellerier <dionoea at videolan dot org> Pierre Ynard This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. --]==========================================================================] running = true function quit() vlc.misc.quit() running = false end --Begin user-defined functions --[[The input buffer is split into the global table "val"; val[0] is the function itself, val[1] ... val[n] is a variable number of arguments. There is no error handling (yet), and you cannot type commands at the commandline; this interface is designed to take machine-generated input from a fifo. Test this interface with: luac5.1 <this file> mkdir -p $HOME/.local/share/vlc/lua/intf mv luac.out $HOME/.local/share/vlc/lua/intf/shell.luac mkfifo fifo exec 3<> fifo vlc <&3 -I "luaintf{intf=shell}" /path/to/media #Command example: echo set_var input time 123.456 > fifo ... ]] -- sample functions for testing (add whatever you need): function set_time() --e.g. set_time 123.456 local val = val[1] local input = vlc.object.input() local check = (vlc.var.get(input,"length") - val) if val and check > 0 then vlc.var.set(input,"time",val) end end function set_var() --e.g. set_var input chapter 1 local obj = val[1] local var = val[2] local value = val[3] --float or int depending on var --it really should work like this: --local string = string.format("%s%s(),\"%s\"","vlc\.object\.",obj,var) local o if obj == "input" then o = vlc.object.input() elseif obj == "aout" then o = vlc.object.aout() elseif obj == "vout" then o = vlc.object.vout() end vlc.var.set(o,var,value) end --write variables to file --[[for importing variables in shell scripts use something like this: get_var() { local i cat /dev/null > $TMP for i in "$@"; do echo "write_var input $i $TMP" > $FIFO done eval `cat $TMP` } ]] function write_var() --e.g. write_var input title <path> local obj = val[1] local var = val[2] local path = val[3] local o if obj == "input" then o = vlc.object.input() elseif obj == "aout" then o = vlc.object.aout() elseif obj == "vout" then o = vlc.object.vout() end local value = vlc.var.get(o,var) local file = io.open(tostring(path), "a+") file:write(tostring(var) .. '=' .. tostring(value) .. '\n') file:close() end --atrack and strack --[[The listvalue function in cli.lua is bogus because elementary streams can change on DVDs when switching back and forth between titlesets. The order of tracks in the track list remains constant though; so we need to grep for the matching track and then set audio-es or spu-es to the value in the 1st column.]] function track() --e.g. track audio-es 3 or track spu-es 5 local var = val[1] local value = val[2] local o = vlc.object.input() local v,l = vlc.var.get_list(o,var) local val for i,val in ipairs(v) do local str = tostring(val)..tostring(l[i]) local str1 = string.format("Track %s", tostring(value)) if string.match(str,str1) then local t = string.match(str,"^%d+") vlc.var.set(o,var,t) break end end end --End user-defined functions --Utility: split buffer at word boundaries --Add stuff to the regex in string.gmatch if needed function split_input(str) local j = 0 val = {} for i in string.gmatch(str,"[%w_/-]+") do --local i not needed val[j] = i --num_args = j j = j+1 end end --??????????????????????????????????????? --Can someone explain what these are for? --function on_read(client) --end --function on_write(client) --end --??????????????????????????????????????? require "host" h = host.host() -- Bypass any authentication function on_password(client) client:switch_status(host.status.read) end h.status_callbacks[host.status.password] = on_password --h.status_callbacks[host.status.read] = on_read --h.status_callbacks[host.status.write] = on_write h:listen("*console") -- The main loop (cribbed from cli.lua and host.lua) while running do -- accept new connections and select active clients local write,read = h:accept_and_select() -- handle clients in write mode for _, client in pairs(write) do client:send() client.buffer = "" client:switch_status(host.status.read) end -- handle clients in read mode for _, client in pairs(read) do local input = client:recv(1000) --[[this seems to be the number of bytes received. If so, could probably be less for a pure console interface]] if not input then break end client.cmds = input .. '\n' client.buffer = client.cmds client:switch_status(host.status.write) split_input(client.buffer) _G[val[0]]() end end
Last edited by nokangaroo on 06 Mar 2015 05:55, edited 2 times in total.

roland1
Blank Cone
Blank Cone
Posts: 44
Joined: 01 May 2014 16:49

Re: A stripped console-only Lua CLI for use with shell scripts

Postby roland1 » 05 Mar 2015 20:02

Nice, here is another approach (using Lua)
save as kpo

Code: Select all

#!/usr/bin/env lua if not ... then os.exit(-1) end local FIFOP = ... local pr while true do pr = pr or io.open(FIFOP) if not pr then os.exit(-1) end local s = pr:read"*a" if not s or #s == 0 then pr:close(); pr = nil else io.write(s); io.flush() end end
run setup

Code: Select all

mkfifo vlc_fifo lua kpo vlc_fifo | vlc -I cli
run command

Code: Select all

echo description > vlc_fifo

nokangaroo
Blank Cone
Blank Cone
Posts: 29
Joined: 30 Dec 2010 17:11

Re: A stripped console-only Lua CLI for use with shell scripts

Postby nokangaroo » 06 Mar 2015 00:31

Thanks!
Can you please explain how your script works? I really don't know jack about Lua; I'm working on my best guess here.
Since this is about documenting the Lua interface, please contribute. I am not really competent to write documentation;
I'm only doing it because I could not find anything useful.

Edit:
Does this mean that there are 2 instances of lua running? And since you use
a shebang line,

kpo vlc_fifo | vlc -I "luaintf{intf=shell}"

should suffice, right? I have to write a suitable script first, or adapt an
existing one, before I can test this. And vlc -I cli only gets me the existing
cli.lua, which I don't like.

roland1
Blank Cone
Blank Cone
Posts: 44
Joined: 01 May 2014 16:49

Re: A stripped console-only Lua CLI for use with shell scripts

Postby roland1 » 06 Mar 2015 02:29

...Yes and ...Yes, but testing could be done by simply running (echo) commands in a second terminal.
Here is a much shorter alternative

Code: Select all

mkfifo vlc_fifo while true; do cat vlc_fifo; done | vlc -I cli
which also desribes what kpo is intended to do.

nokangaroo
Blank Cone
Blank Cone
Posts: 29
Joined: 30 Dec 2010 17:11

Re: A stripped console-only Lua CLI for use with shell scripts

Postby nokangaroo » 06 Mar 2015 05:40

Thanks.


Return to “Scripting VLC in lua”

Who is online

Users browsing this forum: No registered users and 3 guests