How to get the current playing time in vlc Lua extension script - lua

I am new writing lua extension for vlc. I have a bare minimal vlc extension that pops a dialog on activating the menu item and logs message to terminal. How to get the current duration of the file which is playing. Looked at this manual https://www.videolan.org/developers/vlc/share/lua/README.txt but did not help.

You could try using the time VLC variable
such as:
function getTimePassed()
return vlc.var.get(vlc.object.input(), "time")
end
And then use it as:
local elapsedDuration = getTimePassed()
local timeAsString = os.date("%H:%M:%S", elapsedDuration)

From the documentation you referenced:
input.item(): Get the current input item. Input item methods are:
:duration(): Get item's duration in seconds or negative value if unavailable.
So, input.item():duration() should give you the duration you are looking for.

Related

Script can't find tool in backpack

1. Summarize the problem
My script can't find the tool in the player backpack. The output of the print is always nil.
The toll is given by another script before the script that has to search it.
No error message appears.
2. Describe what you've tried
I've tried searching around but nothing fix my problem. I tried re-writing the code from 0 too.
3. Show some code
Simple script Code that clone the tool and place it in the backpack
script.Parent.Triggered:Connect(function(player)
local Frullato = game.ServerStorage.LemonadeStand.Tools.Frullato:Clone()
Frullato.Parent = player.Backpack
local Frappe = require(game.ServerScriptService.DummyMove)
end)
Simple script Code that can't find the tool. Print output is "nil"
script.Parent.Triggered:Connect(function(player)
local Smoothie = player.Backpack:GetChildren("Frullato")
print(Smoothie.name)
end)
Instead of :GetChildren() use :FindFirstChild(), and "Smoothie.name" should be replaced with "Smoothie.Name"

The effect of use_sim_time in ROS

I am trying to understand what effects does setting use_sim_time to true does specially when recording and playing a rosbag, but unfortunately the info is few and hard to understand.
I know already how to set it to true, so that is no problem.how to do it
I have done some experiments with a rosbag file I have and I noticed:
When I do rosplay play file.bag , the topic \clock is not published
When I do rosplay play file.bag --clock the topic \clock is published
I also have noticed that when I do rostopic echo \clock and I play the bag, many of the time published is the same! (what does this mean??)
And lastly I have noticed that use_sim_time has no effect on any of these results.
So what effect does setting this parameter to true have?
In order for a ROS node to use simulation time according to the /clock topic, the /use_sim_time parameter must be set to true before the node is initialized. In other words,
The ROS API used to get times ros::Time time = ros::Time::now() will retrieve time data from the /clock topic rather than using the system clock. If you turn use_sim_time off then any time values published to /clock will be ignored
If the /use_sim_time parameter is set, the ROS Time API will return time=0 until it has received a value from the /clock topic. Then, the time will only be updated on receipt of a message from the /clock topic and will stay constant between updates
More information follow: [1], [2]

QtLua capture slider value change

I am trying to make a simple UI using qtlua, in which I want to capture the slider value everytime it changes. I tried to connect to the valueChanged() signal, but qlua gives me the following error:
cannot find source signal valueChanged()
The code snippet looks like this:
slide = (widget.sliderLight)
print(slide)
qt.connect(slide, 'valueChanged()',
function()
print('Value: ', slide.value)
end)
So just a test to print everytime the value changes. But I cannot get it working. The documentation for the qtlua doesn't have a class for qslider, so that's a dead end for me. And I couldn't find any examples for using qtlua with a slider that is connected to the valueChanged() signal. The only example I found was with the test.lua in the qtuiloader example, but that uses a timer, which I assume is pooled at regular interval. My aim is to hook this up to an image processing system, so it would be useful if I could tie it to when the value changes, rather than patch in with a timer and a check system. I am pretty new to qt, so must be missing something. Any and all help would be really appreciated!
Oh and I made sure I have tracking checkbox checked in the qtdesigner, to ensure that the signal is emitted.
Okay, so I dug around a little more in the documentation of QT for the valueChanged() slot. Turns out, the function signature has an int argument in it, so the Lua connect code was looking for a function signature without any arguments. Modifying the above code to the following works as expected:
slide = (widget.sliderLight)
print(slide)
qt.connect(slide, 'valueChanged(int)',
function(w)
print('Value: ', w)
end)
Declaring a function with the same signature also passes in the required value, which saves me an explicit value query.
Hopefully, this will be useful for someone someday.

Lua delete from playlist - VLC Plugin

I try to delete the current playing file in playlist. I have some plugin in Lua to do that but the file is stil in use at this time.
Therefore I need a short wait in my script.
function nexttrack()
vlc.playlist.delete(itemId)
vlc.playlist.next()
delete()
end
function delete()
os.remove(filename) -- do nothing at this time
end
I have try os.execute(sleep 1) and os.execute(ping ....) but generally Vlc crashed when I try to run os.execute!
When I run delete() from a massagebox the file will get deleted.
Did somebody have a tip to get it working
You could simulate a busy wait with os.time and a loop.
For a sleep/wait of the OS scheduler, Lua 5.1 (which normal builds of VLC use) standard libraries don't have a function.
In case of VLC with LuaJIT you would have the ffi library which offers access to DLL functions, system libraries with sleep included.
Taking a look at the VLC Lua API, misc.mdate and misc.mwait could be what you're searching for.
I have done it with a loop who test/wait for changes in my ItenId value (TrackID in Vlc Playlist)!
function nexttrack()
vlc.playlist.delete(itemId)
vlc.playlist.next()
while probe() == itemId
do
vlc.msg.info("wait")
end
close()
os.remove(filename)
end

"attempt to call global 'tonumber' (a nil value)" in Lua, embedded (in VLC)

I use VLC media player 1.1.9 on Ubuntu 11.04. I'm trying to experiment with lua extensions for VLC; so I've added the file test.lua in ~/.local/share/vlc/lua/extensions/, which has only these two lines:
fps="25.000"
frame_duration=1/tonumber(fps)
When I run vlc with verbose output for debugging, I get (edited to split on multiple lines:):
$ vlc --verbose 2
...
[0xa213874] lua generic warning: Error loading script
~/.local/share/vlc/lua/extensions/test.lua:
.../.local/share/vlc/lua/extensions/test.lua:2:
attempt to call global 'tonumber' (a nil value)
...
Now, as far as I know, tonumber as function is part of Lua5.1 proper (Lua 5.1 Reference Manual: tonumber) - and on my system:
$ locate --regex 'lua.*so.*' | head -4
/usr/lib/libipelua.so.7.0.10
/usr/lib/liblua5.1.so
/usr/lib/liblua5.1.so.0
/usr/lib/liblua5.1.so.0.0.0
... apparently I do have Lua 5.1 installed.
So, why do I get an error on using tonumber here - and how can I use this (and other) standard functions in a VLC lua extension properly?
Documentation is sparse for VLC Lua extensions to say the least but I did find an example in the github vlc repository here: https://github.com/videolan/vlc/blob/master/share/lua/extensions/VLSub.lua
Judging from that example it appears you need to supply some basic event functions for your addon for VLC to call into when certain events happen. Some of the obvious callback handlers I've noticed:
descriptor, this should return a table that contains fields describing your addon.
activate, this seems to get called when you activate it from view menubar.
deactivate, called when you deactivate the addon from view menubar.
plus a couple of other functions like close and input_change which you can guess what they're for.
From my brief testing done on VLC 2.0.8 under Win7 it appears VLC loads the lua extension using an empty sandbox environment. This is likely the reason you're getting nil for tonumber and I'm betting none of the other standard lua functions are accessible either when you try to perform computation at this global scope.
However, if I move that code into one of the event handling functions then all those standard functions are accessible again. For example:
function descriptor()
return
{
title = "Test Ext";
version = "0.1";
author = "";
shortdesc = "Testing Lua Extension";
capabilities = {};
description = "VLC Hello Test Addon";
}
end
function activate()
print "test activating"
local fps = tonumber "25.000"
local frame_duration = 1 / fps
print(frame_duration)
return true
end
-- ...
That prints out what you would expect in the console debug log. Now the documentation (what little there is) doesn't mention any of this but what's probably happening here is VLC is injecting the standard lua functions and vlc api table into the sandboxed environment when any of these event handlers get called. But during the extension loading phase, it is done in an empty sandbox environment which explains why all those lua function calls end up being nil when you try to use it at the outter most scope.
I recommend cloning the VLC source tree from github and then performing a grep on the C source that's embedding lua to see what VLC is really doing behind the scenes. Most of the relevant code will likely be here: https://github.com/videolan/vlc/tree/master/modules/lua
Probably some extension script installed in your system overwrites the function and the Lua interpreter instance is shared between all extension scripts, so you end up not being able to call the function if that script is called before yours.
As a quick workaround, Lua being dynamically typed, you can still do things like:
1 / "25.000"
and the string will be coerced to a number.
Alternatively, you can define a tonumber equivalent like:
string_to_num = function(s) return s + 0 end
This again relies on dynamic typing.

Resources