Search code examples
luavlc

Building a playlist to control playback options for each media file individually in VLC/Lua


I have a playlist containing video files. For each playlist item I want to control the playlist mode for whether each track should "repeat", "play-and-stop" etc. in VLC using a Lua script.

file:///data/video1.mp4,repeat
file:///data/video2.mp4,play-and-stop

The aim is that some video tracks should repeat indefinitely until the user manually advances to the next track. Other tracks in the playlist should play once and then advance to the next track, or play-and-stop and wait for the user to interact before play commences again.

I currently have the following code adapted from here, however I'm unable to apply the playlist options to each track individually (the options apply to the whole playlist). Is there any way I can extend my script to achieve this?

function probe()
    return string.match(vlc.path, "%.myplaylist$")
end

function parse()
    playlist = {}

    while true do
        playlist_item = {}
        line = vlc.readline()
        if line == nil then
            break

        -- parse playlist line into two tokens splitting on comma
        values = {}
        i=0
        for word in string.gmatch(line, '([^,]+)') do
            values[i]=word
            i=i+1
        end

        playlist_item.path = values[0]
        playback_mode = values[1]

        playlist_item.options = {}
        table.insert(playlist_item.options, "fullscreen")
        table.insert(playlist_item.options, playback_mode)

        -- add the item to the playlist
        table.insert( playlist, playlist_item )
    end

    return playlist
end

Adding "video options" to playlist_item.options is working, but adding "playlist options" on a per track basis does not. I'm unsure how to proceed as I can only return an entire playlist.


Solution

  • Given a custom playlist

    file:///data/video1.mp4,repeat
    file:///data/video2.mp4,play-once
    

    I completed the playlist script in the original question above by adding the repeat/play-once information to the track metadata.

    playlist_item.meta = { ["Playback mode"] = playback_mode }
    

    The final step was to create an extension (similar to the Song Tracker extension) that listens to the input_changed event and uses the "Playback mode" track metadata to toggle vlc.playlist.repeat_() accordingly.

    function activate()
      update_playback_mode()
    end
    
    function input_changed()
      update_playback_mode()
    end
    
    function update_playback_mode()
      if vlc.input.is_playing() then
        local item = vlc.item or vlc.input.item()
        if item then
          local meta = item:metas()
          if meta then
            local repeat_track = meta["Playback mode"]
            if repeat_track == nil then
              repeat_track = false
            elseif string.lower(repeat_track) == "repeat" then
              repeat_track = true
            else
              repeat_track = false
            end
    
        local player_mode = vlc.playlist.repeat_()
    
        -- toggle playlist.repeat_() as required        
        if player_mode and not repeat_track then
            vlc.playlist.repeat_()
        elseif not player_mode and repeat_track then
            vlc.playlist.repeat_()
        end
    
        return true
      end
    end