[Extension] Playlist randomizer

Discuss your Lua playlist, album art and interface scripts.
mederi
Big Cone-huna
Big Cone-huna
Posts: 1951
Joined: 15 Mar 2011 16:38
VLC version: 2.0.8
Operating System: Windows Vista/XP

[Extension] Playlist randomizer

Postby mederi » 03 Apr 2012 20:11

Recently I have read following post: Re: Playlist - randomize order in Feature Requests section.
I have thought that it could be possible to make some simple extension for this. I have checked VLC's Lua README file and found appropriate function that I could need:

Code: Select all

vlc.playlist.sort('random') vlc.playlist.get( [what, [tree]] ) vlc.playlist.clear() vlc.playlist.enqueue( ... )
.sort() and .clear() work. Now I would like to ask you to help me to find out, how to read the whole playlist using the .get() function. How to read all playlist items (id, path, name) and temporarily store them in a table (array)?
I have tried this:

Code: Select all

items=vlc.playlist.get() vlc.msg.info(tostring(items.path)) for idx, details in ipairs(items) do vlc.msg.info(idx.."-"..details.id) vlc.msg.info(idx.."-"..details.path) end
It does not work. Any idea?
Thanks.

--- EDIT (10.4.2012) ---
topic's new title: "VLC Extension: Playlist randomizer"
old one: "How to read all playlist items?"
Last edited by mederi on 10 Apr 2012 16:10, edited 2 times in total.

nkoriyama
Cone that earned his stripes
Cone that earned his stripes
Posts: 338
Joined: 01 Sep 2011 20:50
VLC version: git
Operating System: Windows / Mac OS X
Location: Japan

Re: How to read all playlist items?

Postby nkoriyama » 05 Apr 2012 02:38

How To Ask Questions The Smart Way http://www.catb.org/~esr/faqs/smart-questions.html
My hack for ISDB-T http://sdrv.ms/126weue

mederi
Big Cone-huna
Big Cone-huna
Posts: 1951
Joined: 15 Mar 2011 16:38
VLC version: 2.0.8
Operating System: Windows Vista/XP

Re: How to read all playlist items?

Postby mederi » 10 Apr 2012 15:49

nkoriyama, thank you for the clue. The ".children" does the trick. Here is the script that I have made first:

Code: Select all

--- "playlist randomizer - basic.lua" --- function descriptor() return { title = "Randomize playlist (basic)"; } end function activate() vlc.playlist.sort('random') randomized_playlist=vlc.playlist.get("playlist",false).children new_playlist={} for i, v in pairs(randomized_playlist) do table.insert(new_playlist, {path=v.path, title=v.name, duration=v.duration}) end vlc.playlist.clear() vlc.playlist.enqueue(new_playlist) vlc.deactivate() end function deactivate() end
VLC often crashes and freezes with it. I have tried to identify and eliminate all factors causing troubles: empty playlist, running playback, reading of metadata (duration, album, ...), dialog box.

Here is the second script that should work better than the basic one. However, before activating this extension, rather manually stop playback if it is running. If you fresh feed the playlist, you should also wait for VLC to finish reading of metadata of all playlist items. Then take a deep breath, keep your fingers crossed and activate the extension :D

Code: Select all

--- "playlist randomizer.lua" --- function descriptor() return { title = "Randomize playlist"; } end function activate() if (#vlc.playlist.get("playlist",false).children > 0) then if vlc.input.is_playing()==false then vlc.msg.info("[Playlist randomizer] playlist not empty, no playback, ready for randomizing") click_Randomize() else vlc.msg.info("[Playlist randomizer] playlist not empty, stopping actual playback, opening dialog box") vlc.playlist.stop() create_dialog() end else vlc.msg.info("[Playlist randomizer] empty playlist, deactivating extension") vlc.deactivate() end end function deactivate() end function close() vlc.deactivate() end function create_dialog() d = vlc.dialog("Playlist randomizer") button = d:add_button("Randomize playlist", click_Randomize,1,2,1,1) end function click_Randomize() if vlc.input.is_playing()==true then vlc.msg.info("[Playlist randomizer] dialog box opened, stopping actual playback???") vlc.playlist.stop() --return end vlc.msg.info("[Playlist randomizer] randomizing") vlc.playlist.sort('random') randomized_playlist=vlc.playlist.get("playlist",false).children new_playlist={} for i, v in pairs(randomized_playlist) do table.insert(new_playlist, {path=v.path, title=v.name, name=v.name, duration=v.duration}) end vlc.playlist.clear() vlc.playlist.enqueue(new_playlist) vlc.msg.info("[Playlist randomizer] randomizing done, deactivating extension") vlc.deactivate() end
VLC's Lua extension module really need some maintenance and improvements.

str3tmonk
Blank Cone
Blank Cone
Posts: 40
Joined: 21 Jun 2009 04:43

Re: VLC Extension: Playlist randomizer

Postby str3tmonk » 07 Nov 2012 21:36

Can this be used for VLM playlists?

mederi
Big Cone-huna
Big Cone-huna
Posts: 1951
Joined: 15 Mar 2011 16:38
VLC version: 2.0.8
Operating System: Windows Vista/XP

VLC Extension: Random Songs

Postby mederi » 08 Mar 2013 16:04

random_songs.lua << smoak's interesting sctipt, that feeds Playlist with media files from Media Library in random order.

mederi
Big Cone-huna
Big Cone-huna
Posts: 1951
Joined: 15 Mar 2011 16:38
VLC version: 2.0.8
Operating System: Windows Vista/XP

New Random with Loop all

Postby mederi » 07 Jul 2013 19:51

Here is another attempt:

Code: Select all

-- "New Random with Loop all.lua" -- VLC Extension first_ID = 6 items = 10 random_order={3,5,8,2,9,4,7,1,10,6} function descriptor() return { title = "New Random with Loop all", capabilities = {"input-listener", "meta-listener"} } end function activate() create_dialog() end function deactivate() end function close() vlc.deactivate() end k=0 function input_changed() state = vlc.var.get(vlc.object.input(), "state") --vlc.msg.info("["..descriptor().title.."] Input state = "..state) if state==4 then k=k+1 if k>#random_order then k=1 New_Random() end vlc.playlist.goto(random_order[k]+first_ID-2) --vlc.msg.info("["..descriptor().title.."] ".. k ": goto(" .. random_order[k] .. "+"..first_ID.."-2)") order="" for ix, vx in ipairs(random_order) do if ix==k then vx="<b>["..vx.."]</b>" end order=order..vx..", " end w8:set_text(order) w:update() end end function meta_changed() end ----- Dialog box: ----- function create_dialog() w = vlc.dialog(descriptor().title) w1 = w:add_label("ID of the first item:", 1, 1) w2 = w:add_text_input(first_ID, 2, 1) w3 = w:add_button("Check ID:",click_CheckID, 3, 1) w4 = w:add_label("Number of items:", 1, 2) w5 = w:add_text_input(items, 2, 2) w6 = w:add_label("Random order", 1, 3, 2) w7 = w:add_button("START",click_Start, 3, 3) w8 = w:add_html("", 1, 4, 3) end function click_CheckID() w3:set_text("Check ID: " .. vlc.playlist.current()) end function click_Start() k=0 -- playback reset New_Random() order = "" for _, v in ipairs(random_order) do order = order..v..", " end w8:set_text(order) end ----- Generator of random order: ----- i=0 function New_Random() i=i+1 first_ID = w2:get_text() items = w5:get_text() random_order = {} for j=1,items do random_order[j] = j end -- Initialize the pseudo random number generator math.randomseed( os.time() ) math.random(); math.random(); math.random() -- done. :-) random_order = shuffle(random_order) w6:set_text("Random order #"..i) end function shuffle(t) local n = #t while n > 2 do -- n is now the last pertinent index local m = math.random(n) -- 1 <= m <= n -- Quick swap t[n], t[m] = t[m], t[n] n = n - 1 end return t end
INSTRUCTIONS:
* feed VLC playlist with files;
* VLC Random is off, Loop is off, start to play the first file;
* activate the extension in VLC menu;
* press [Check ID:] button;
* write the ID in the text input field (usually value 5 or 6);
* how many files are there in the playlist (you can activate ID column in VLC playlist (right-click on the header) in Detailed View);
* press [START] button;
* stop the playback of the first file in the playlist;
=> playback continues in random order :)
A new random order will be automatically generated at the end of the last file in current random order or you can press [START] button again anytime.
Starting VLC with --play-and-stop CLI parameter could help (desktop shortcut icon or batch file). Appropriate setting in VLC preferences: Tools > Preferences > ( Show settings = All ) > Playlist: [v] Play and stop >> Save changes, restart VLC.

mandrilltiger
New Cone
New Cone
Posts: 1
Joined: 08 Jun 2014 13:32

Re: VLC Extension: Playlist randomizer

Postby mandrilltiger » 08 Jun 2014 13:46

=> playback continues in random order :)
This is the part where it stops working anyone that can help will be my hero.

mederi
Big Cone-huna
Big Cone-huna
Posts: 1951
Joined: 15 Mar 2011 16:38
VLC version: 2.0.8
Operating System: Windows Vista/XP

Re: VLC Extension: Playlist randomizer

Postby mederi » 09 Jun 2014 10:15

You better use Sampler (PG) extension to shuffle tracks in playlist.


Return to “Scripting VLC in lua”

Who is online

Users browsing this forum: No registered users and 1 guest