[Need help]: Compose m3u8 playlist

Discuss and share scripts and script files...
Post Reply
binocular222
Posts: 1423
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

[Need help]: Compose m3u8 playlist

Post 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?
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

highend
Posts: 14940
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: [Need help]: Compose m3u8 playlist

Post 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).
One of my scripts helped you out? Please donate via Paypal

binocular222
Posts: 1423
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Re: [Need help]: Compose m3u8 playlist

Post by binocular222 »

Thanks, that works.
regexmatches() always give me headache.
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

highend
Posts: 14940
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: [Need help]: Compose m3u8 playlist

Post by highend »

Speed difference in seconds is now?
One of my scripts helped you out? Please donate via Paypal

binocular222
Posts: 1423
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Re: [Need help]: Compose m3u8 playlist

Post by binocular222 »

2s now (5-7 secs previously)
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

highend
Posts: 14940
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: [Need help]: Compose m3u8 playlist

Post by highend »

:tup:
One of my scripts helped you out? Please donate via Paypal

Post Reply