Search code examples
luavlc

Jump to a specific time in a file?


I'm trying to make a VLC script that checks if the "random" button is on, and if so when it jumps to a random file, instead of starting at time=0, it starts at a random time.

So far, it's looking to me like it should be a playlist script and I can get the duration from the playlist object, but in this documentation page doesn't show how to jump to a specific time from within the Lua script.

How can that be done in Lua?


Solution

  • Actually, the documentation does say you can do it...though not in so many words. Here's what it says about the interface for playlist parsers:

    VLC Lua playlist modules should define two functions:
       * probe(): returns true if we want to handle the playlist in this script
       * parse(): read the incoming data and return playlist item(s)
            Playlist items use the same format as that expected in the
            playlist.add() function (see general lua/README.txt)
    

    If you follow through to the description of playlist.add() it says the items have a big list of fields you can provide. There are plenty of choices (.name, .title, .artist, etc.) But the only required one seems to be .path...which is "the item's full path / URL".

    There's no explicit mention of where to seek, but one of the parameters you can choose to provide is .options, said to be "a list of VLC options. It gives fullscreen as an example. If a parallel to --fullscreen works, can other command-line options like --start-time and --stop-time work as well?

    On my system they do, and here's the script!

    -- randomseek.lua
    --
    -- A compiled version of this file (.luac) should be put into the proper VLC
    -- playlist parsers directory for your system type.  See:
    --
    --   http://wiki.videolan.org/Documentation:Play_HowTo/Building_Lua_Playlist_Scripts
    --
    -- The file format is extremely simple and is merely alternating lines of
    -- filenames and durations, such as if you had a file "example.randomseek"
    -- it might contain:
    --
    --     foo.mp4
    --     3:04
    --     bar.mov
    --     10:20
    --
    -- It simply will seek to a random location in the file and play a random
    -- amount of the remaining time in the clip.
    
    function probe()
        -- seed the random number since other VLC lua plugins don't seem to
        math.randomseed(os.time())
    
        -- tell VLC we will handle anything ending in ".randomseek"
        return string.match(vlc.path, ".randomseek$")
    end
    
    function parse()
        -- VLC expects us to return a list of items, each item itself a list
        -- of properties
        playlist = {}
    
        -- I'll assume a well formed input file but obviously you should do
        -- error checking if writing something real
        while true do
           playlist_item = {}
    
           line = vlc.readline()
           if line == nil then
               break --error handling goes here
           end
    
           playlist_item.path = line
    
           line = vlc.readline()
           if line == nil then
               break --error handling goes here
           end
    
           for _min, _sec in string.gmatch( line, "(%d*):(%d*)" )
               do
                   duration = 60 * _min + _sec
               end
    
           -- math.random with integer argument returns an integer between
           -- one and the number passed in inclusive, VLC uses zero based times
           start_time = math.random(duration) - 1
           stop_time = math.random(start_time, duration - 1)
    
           -- give the viewer a hint of how long the clip will take
           playlist_item.duration = stop_time - start_time
    
           -- a playlist item has another list inside of it of options
           playlist_item.options = {}
           table.insert(playlist_item.options, "start-time="..tostring(start_time))
           table.insert(playlist_item.options, "stop-time="..tostring(stop_time))
           table.insert(playlist_item.options, "fullscreen")
    
           -- add the item to the playlist
           table.insert( playlist, playlist_item )
        end
    
        return playlist
    end