Page 1 of 1

[Need help]: Compose m3u8 playlist

Posted: 26 Oct 2015 06:59
by binocular222
I have a music folder with a lot of XY tags. I want to write each tag to a playlist file, i.e: file Instrumental.m3u8 list all files having the tag "Instrumental".
The tricky thing is:
1) .m3u8 only accept UTF-8 and UTF-8 with BOM while XY writefile() only accept ANSI and UTF-8 LE
2) That folder have A LOT of files, thus The follwing script is too slow to retrieve the list of files having the same tag:

Code: Select all

	$allitems = report("<curpath>\{Name}|");
	$TagList = formatlist(report("{Tags}, "),"nde", ", ");
	foreach ($tag, $TagList, ", ") {
		$List = "";
		foreach($item, $allitems) {
			if( gettokenindex($tag, property("#tags", $item), ", ", "i")) {
				$List = $List . "<crlf>" . $item;
			}
		}
		writefile("<curpath>\$tag.m3u8", $List, , "tu");
	};
Anyone have a more efficient code?

Re: [Need help]: Compose m3u8 playlist

Posted: 26 Oct 2015 09:00
by highend
Don't use two reports, just gather all info ({Fullname} + {Tags}) in one

Don't use the inner foreach loop, instead regexescape the current tag (if necessary) and use a regexmatch to
gather all files with that tag. Reformat that result, write it to the file.

Untested, no time to build any sample data:

Long version:

Code: Select all

   $allInfo = report("{FullName}?{Tags}<crlf>");
   $allTags = regexmatches($allInfo, "\?.*?(?=$)");

   foreach ($tag, formatlist(replace($allTags, "?"), "nde", "|")) {
      $list = "";
      $escTag = regexreplace($tag, "([\\^$.+*|?(){\[])", "\$1");
      $matches = regexmatches($allInfo, "^.*?\?.*?$escTag.*?(?=$)", "<crlf>");
      if ($matches) {
        $list = $list . "<crlf>" . formatlist(regexreplace($matches, "\?.*(?=$)"), "nde", "<crlf>");
      }
      writefile("<curpath>\$tag.m3u8", $list, , "tu");
   }
If all tags don't need to be escaped and all items contain at least one tag, an even shorter version could be used:

Code: Select all

   $allInfo = report("{FullName}?{Tags}<crlf>");
   $allTags = regexmatches($allInfo, "\?.*?(?=$)");

   foreach ($tag, formatlist(replace($allTags, "?"), "nde", "|")) {
      $list = "";
      $matches = regexmatches($allInfo, "^.*?\?.*?$tag.*?(?=$)", "<crlf>");
      $list = $list . "<crlf>" . formatlist(regexreplace($matches, "\?.*(?=$)"), "nde", "<crlf>");
      writefile("<curpath>\$tag.m3u8", $list, , "tu");
   }
The rule of thumb is: Always try to replace a foreach loop (with ENOUGH entries) with a regexmatch (if possible).

Re: [Need help]: Compose m3u8 playlist

Posted: 26 Oct 2015 15:34
by binocular222
Thanks, that works.
regexmatches() always give me headache.

Re: [Need help]: Compose m3u8 playlist

Posted: 26 Oct 2015 15:44
by highend
Speed difference in seconds is now?

Re: [Need help]: Compose m3u8 playlist

Posted: 26 Oct 2015 16:02
by binocular222
2s now (5-7 secs previously)

Re: [Need help]: Compose m3u8 playlist

Posted: 26 Oct 2015 16:09
by highend
:tup: