Function: CreatePlaylist()

Discuss and share scripts and script files...
Post Reply
highend
Posts: 7645
Joined: 06 Feb 2011 00:33

Function: CreatePlaylist()

Post by highend » 27 Dec 2016 00:37

A function to create a playlist (m3u / pls format)
If no files are selected it will create a playlist of all .mp3 files in the current folder
Standard name is "playlist.(m3u/pls)" but you can call it with your own name (without extension)
By default all filenames are without a path but you can force it to use full paths ($portable=false)

Code: Select all

function CreatePlaylist($type="m3u", $name, $files, $portable=true) {
    end !regexmatches($type, "^(m3u|pls)$"), "Unknown playlist format, aborted!";
    $fileSep = (regexmatches($files, "\r?\n")) ? <crlf> : "|";

    if !($name)  { $name = "playlist"; }
    if !($files) { $files = quicksearch("*.mp3 /n", <curpath>, $fileSep); }
    $itemCnt = gettoken($files, "count", $fileSep);

    $header = "#EXTM3U<crlf>";
    if ($type LikeI "pls") { $header = "[playlist]<crlf>"; }

    $playlist = "";
    while ($i++ < $itemCnt) {
        $item   = gettoken($files, $i, $fileSep);
        $ext    = gpc($item, "ext");
        $base   = gpc($item, "base");
        $title  = property("#tag.title", $item);
        $artist = property("#tag.artist", $item);
        $length = regexmatches(property("#audio.length", $item), "^[0-9:]+");
        $secs   = gettoken($length, -1, ":");
        $mins   = gettoken($length, -2, ":");
        $hours  = gettoken($length, -3, ":");
        $time   = ($hours * 60 * 60) + ($mins * 60) + $secs;

        $fileName = ($portable) ? "$base.$ext" : $item;
        if ($type LikeI "m3u") { $playlist = $playlist . "#EXTINF:$time,$artist - $title<crlf>$fileName<crlf>"; }
        else { $playlist = $playlist . "File$i=$fileName<crlf>Title$i=$artist - $title<crlf>Length$i=$time<crlf>"; }
    }
    if ($playlist) {
        if ($type LikeI "pls") { $playlist = trim($playlist, <crlf>, "R") . "<crlf>NumberOfEntries=$itemCnt<crlf>Version=2<crlf>"; }
        writefile("$name.$type", $header . $playlist, , "utf8");
    }
    return;
}
One of my scripts helped you out? Please donate via Paypal or highend (at) web (dot) de

Dustydog
Posts: 248
Joined: 13 Jun 2016 04:19

Re: CreatePlaylist()

Post by Dustydog » 29 Sep 2017 01:10

This doesn't seem to work with selected files, only all in the directory. Wouldn't $files require a list separated by "|"? Or am I missing something?

Also, adding the line:

if !($files) { $files = quicksearch("*.flac /n", <curpath>, $fileSep); }

next to the one for .mp3s makes it a little handier. (Couldn't figure out, quickly, how to do an "OR" in that line for ".flac", though it works in quicksearch live with a bar. Also, why the "/n"? I couldn't see why it was necessary.

(When I get around to it, unless you do, I plan on allowing an option to append the relative parent directory, and write the playlist there with the directory name as the playlist name as I find this the most useful for playlists for multiple discs. I usually do this with a regex in Editpad; I should script it. I usually don't bother with playlists except in that situation, though I've been doing it more often lately with this.)

This is kind of a fun script, thank you. It's also good for checking one's metadata for being reasonably consistent with the filename visually. It's good to find little surprises when I ripped my disks and wasn't watching like "Сергей Васильевич Рахманинов" instead of Rachmaninov - even if it is more correct. Beats the filename-only playlists I tend to create, when I do bother. (Setting .m3u as a text preview in XY is very handy, of course.)

highend
Posts: 7645
Joined: 06 Feb 2011 00:33

Re: CreatePlaylist()

Post by highend » 01 Oct 2017 17:46

This doesn't seem to work with selected files, only all in the directory
Mh? How did you call the function exactly?
Wouldn't $files require a list separated by "|"? Or am I missing something?
The function checks if you supply a | or <crlf> separated list
Also, why the "/n"?
The fallback is using all mp3 files in the current directory. Not in all subdirectories!
I plan on allowing an option to append the relative parent directory, and write the playlist there with the directory name as the playlist name as I find this the most useful for playlists for multiple discs
You can choose the name you want as a param and just move the created playlist file anywhere you want after calling the function?
One of my scripts helped you out? Please donate via Paypal or highend (at) web (dot) de

Dustydog
Posts: 248
Joined: 13 Jun 2016 04:19

Re: CreatePlaylist()

Post by Dustydog » 08 Oct 2017 19:19

Did something daft with my call, of course; my passed list is correct now.

Should have checked command syntax more closely before asking a question and wasting your time.

Added an OR to get .flac or .mp3s or both if no list passed. Added another parameter to get the behavior I wanted for a relative-path list in the parent directory (which is often what I need) and a few other tweaks. Appreciate your work very much.

When I say something like "Doesn't seem to work." it means, "OK, I'm being an idiot. What am I probably doing wrong?" Your scripts are clean, elegant, and extremely well coded. They're great to go through and learn something from. I don't intend on acting like I've got a clue most of the time, let alone a criticism. Should have been more careful with my language. Apologies.

highend
Posts: 7645
Joined: 06 Feb 2011 00:33

Re: CreatePlaylist()

Post by highend » 08 Oct 2017 19:33

Added another parameter to get the behavior I wanted for a relative-path list in the parent directory (which is often what I need)
Show it, still don't know what's meant...
and a few other tweaks
Like?
One of my scripts helped you out? Please donate via Paypal or highend (at) web (dot) de

lian00
Posts: 192
Joined: 09 Jul 2014 17:12

Re: CreatePlaylist()

Post by lian00 » 05 Aug 2018 17:25

Hello, sorry for my noobish question but how make it work with a button ? I copied the code but the entry in my menu button is grey.
Windows 10 64 bits

highend
Posts: 7645
Joined: 06 Feb 2011 00:33

Re: CreatePlaylist()

Post by highend » 05 Aug 2018 17:40

It's a function. You need to call it.
In it's most basic form via:

Code: Select all

CreatePlaylist();
One of my scripts helped you out? Please donate via Paypal or highend (at) web (dot) de

lian00
Posts: 192
Joined: 09 Jul 2014 17:12

Re: CreatePlaylist()

Post by lian00 » 05 Aug 2018 17:49

highend wrote:It's a function. You need to call it.
In it's most basic form via:

Code: Select all

CreatePlaylist();
Thanks.
Windows 10 64 bits

Post Reply