Function: CreatePlaylist()

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

Function: CreatePlaylist()

Post by highend »

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 paypal_donate (at) stdmail (dot) de

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

Re: CreatePlaylist()

Post by Dustydog »

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: 13260
Joined: 06 Feb 2011 00:33

Re: CreatePlaylist()

Post by highend »

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 paypal_donate (at) stdmail (dot) de

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

Re: CreatePlaylist()

Post by Dustydog »

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: 13260
Joined: 06 Feb 2011 00:33

Re: CreatePlaylist()

Post by highend »

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 paypal_donate (at) stdmail (dot) de

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

Re: CreatePlaylist()

Post by lian00 »

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: 13260
Joined: 06 Feb 2011 00:33

Re: CreatePlaylist()

Post by highend »

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 paypal_donate (at) stdmail (dot) de

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

Re: CreatePlaylist()

Post by lian00 »

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

kotlmg
Posts: 298
Joined: 30 Jun 2010 17:14

Re: Function: CreatePlaylist()

Post by kotlmg »

sir above code unable to display file names when playing the media player. it is showing numbers only.
i have selected only 5 files but the above code created playlist of 158 files which i have not selected.
how to include all media and audio files in the above code.
can we send the above list to default media player automatically?

with regards,
kotlmg

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

Re: Function: CreatePlaylist()

Post by highend »

above code unable to display file names when playing the media player.
It creates a playlist file. Don't know what your player makes out of it. Mine show file names just fine.
i have selected only 5 files but the above code created playlist of 158 files which i have not selected
Again, it's a function. Call it correctly^^
how to include all media and audio files in the above code
By reading the help file about generic file types and change the quicksearch line appropriately
can we send the above list to default media player automatically?
Use open for the created file in your code?
One of my scripts helped you out? Please donate via Paypal or paypal_donate (at) stdmail (dot) de

Post Reply