Skip to content

Instantly share code, notes, and snippets.

@bitsydoge
Created October 26, 2023 14:11
Show Gist options
  • Save bitsydoge/82c66d60913cfab1152e31f8b492be31 to your computer and use it in GitHub Desktop.
Save bitsydoge/82c66d60913cfab1152e31f8b492be31 to your computer and use it in GitHub Desktop.
function descriptor()
return {
title = "Shuffle Playlist & Save",
version = "1.0",
shortdesc = "Shuffle Playlist",
description = "Shuffles all items on the playlist and save the playlist",
author = "Doge",
capabilities = {"trigger"}
}
end
function shuffle_playlist(playlist)
math.randomseed(os.time() + os.clock() * 1000)
local randomList = {}
for i, v in ipairs(playlist.children) do
if v.duration == -1 then
v.duration = 0
end
randomList[#randomList + 1] = v
end
local n = #randomList
for i = n, 2, -1 do
local j = math.random(1, i)
randomList[i], randomList[j] = randomList[j], randomList[i]
end
return randomList
end
function save_playlist_to_m3u(filepath, playlist)
local file = io.open(filepath, "w")
if file then
file:write("#EXTM3U\n")
for _, item in ipairs(playlist) do
file:write("#EXTINF:" .. item.duration .. "," .. item.name .. "\n")
file:write(item.path .. "\n")
end
file:close()
vlc.msg.info("Playlist saved to " .. filepath)
else
vlc.msg.err("Failed to save playlist to " .. filepath)
end
end
function trigger()
vlc.playlist.stop()
local pl = vlc.playlist.get("normal", false)
local shuffledList = shuffle_playlist(pl)
local savePath = "PathToYourPlaylist.m3u"
save_playlist_to_m3u(savePath, shuffledList)
shuffledList = shuffle_playlist(pl)
vlc.playlist.clear()
vlc.playlist.enqueue(shuffledList)
vlc.playlist.play()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment