var.add_callback usage

Discuss your Lua playlist, album art and interface scripts.
khuankhuan
Blank Cone
Blank Cone
Posts: 13
Joined: 02 Nov 2011 17:34

var.add_callback usage

Postby khuankhuan » 20 Feb 2012 19:13

I am currently trying to do a lua extension that can almost precisely pause the video at 0.04s before the next second (e.g: pause at 4.96s before entering 5s).

My current method is keep on reading to the variable "time" and pause accordingly, but this is an iterative try method which requires a few tries before success.

I had read something about var.add_callback but not sure on how to use it. Can I use something like vlc.var.add_callback(vlc.object.input(), "time", pause, VAR) to initiate a callback to function "pause" when the time reaches 4.96? and what should I put for the VAR?

Thank you for your help, urgent reply needed, thanks a million!

tittbit
Blank Cone
Blank Cone
Posts: 17
Joined: 10 Mar 2012 19:40

Re: var.add_callback usage

Postby tittbit » 21 May 2012 20:26

why do u need this function, i mean are you building an app or embedding vlc in some software.

i want to say why anyone will want to pause video at 4.96s instead of 4 or 5 sec.
Life is not finding yourself, it is discovering yourself.

Vhati
Blank Cone
Blank Cone
Posts: 16
Joined: 06 Apr 2011 08:07

Re: var.add_callback usage

Postby Vhati » 22 May 2012 20:16

Can I use something like vlc.var.add_callback(vlc.object.input(), "time", pause, VAR)
Input's time doesn't work, but you can do this:

Code: Select all

--Global setting for minimum seconds between callbacks. --desired_interval = 5.00 function activate() input_callback("add") end function deactivate() input_callback("delete") end function input_changed() input_callback("add") --Probably safe to redundantly add, and a nil input is handled in the func. end function input_callback(action) assert(action == "add" or action == "delete") local input = vlc.object.input() if input and action == "add" then vlc.var.add_callback(input, "intf-event", my_callback, "Hello world!") elseif input and action == "delete" then vlc.var.del_callback(input, "intf-event", my_callback, "Hello world!") end --"intf-event" triggers about 5x/second. end last_call = 0 function my_callback(var_name, old_value, new_value, user_data) --Ordinarily one would do this to only act every N seconds. --if os.clock() - last_call < desired_interval then return end last_call = os.clock() --Do something periodically end
VAR is any custom info you want, in case you use a single callback for multiple events and need to differentiate them, or something.
When you deactivate, you'll have to give the exact same args to del_callback(), so it's convenient to manage add and delete in the same place.

This isn't granular enough for 0.04s accuracy however. But since os.clock has subsecond accuracy, track the average elapsed time between callbacks, and when ({math.modf(vlc.input.time)})[2] is nearer to 0.96 than your average callback gap, do a CPU-abusing while loop until the moment arrives (or abort if it passes by).


Return to “Scripting VLC in lua”

Who is online

Users browsing this forum: No registered users and 2 guests