Function: CreatePlaylist()
Posted: 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)
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;
}