Page 1 of 1

Calculate playing time of mp3's

Posted: 06 Jan 2010 20:34
by aurumdigitus
Using Sticky Selection would like to be able to pick a number of mp3's in a folder and have the total playing time (Length) calculated from the Tags and displayed in some manner. Can this be accomplished via Scripting, conjuring, incantation or any other manner?

Re: Calculate playing time of mp3's

Posted: 06 Jan 2010 20:52
by admin
aurumdigitus wrote:Using Sticky Selection would like to be able to pick a number of mp3's in a folder and have the total playing time (Length) calculated from the Tags and displayed in some manner. Can this be accomplished via Scripting, conjuring, incantation or any other manner?
I think scripting is enough, but incantation never harms.

I would look at the property() function, and then it's just a little string parsing and math.

Re: Calculate playing time of mp3's

Posted: 06 Jan 2010 21:54
by TheQwerty

Code: Select all

//Display the Length Property # of the current system.
"Find Length Property : showProp"
	Global($prop);
	Sub("_getProp");
	$prop = $prop == 100 ? "Could not find length property for the current item." : "The Length Property is #$prop.";
	Echo($prop);

//Brute Force through Properties to find the length.
"_getProp"
	Global($prop);
	$prop = 1;
	while ($prop < 100) {
		$test = RegExReplace(Property("#$prop", "<curitem>"), "^[0-9]+:[0-9]+:[0-9]+$", ":::MATCH:::");
		if ($test Like ":::MATCH:::") { break; }
		$prop++;
	}

"Calculate Length of Selection"
	/* Find the Property number corresponding to the length.
		You can remove the next three lines if you replace "$prop" below
		with the number returned by "Find Length Property".
	*/
	Global($prop);
	Sub("_getProp");
	End($prop == 100, "Could not find length property of:<crlf><curitem>");

	//Convert the length of each item to seconds and sum them.
	Global($sec);
	$result = Report('Eval(RegExReplace(Property("#' . $prop . '","{FullName}"),"^([0-9]+):([0-9]+):([0-9]+)$","$1*3600+$2*60+$3"))+',1,'Global($sec);$sec=Eval(','0);');
	Load("$result",,"s");

	//Convert from seconds back to hh:mm:ss
	$hrs = '00';
	$min = '00';

	if ($sec >= 3600) {
		$hrs = GetToken($sec / 3600, 1, '.');
		$sec = $sec - $hrs*3600;
		$hrs = $hrs < 10 ? "0$hrs" : $hrs;
	}

	if ($sec >= 60) {
		$min = GetToken($sec / 60, 1, '.');
		$sec = $sec - $min*60;
		$min = $min < 10 ? "0$min" : $min;
	}

	$sec = $sec < 10 ? "0$sec" : $sec;

	Echo("Total Length: $hrs:$min:$sec");
For it to work at all <curitem> (the currently focused and selected item) must be a file with a length property. (Unless you edit it to reference the property number as explained below.)

You can make it a bit shorter if you use "Find Length Property" to get the number of the length property on your system and then remove the first 3 lines of code in "Calculate Length of Selection" and replace "$prop" with that number.

Re: Calculate playing time of mp3's

Posted: 06 Jan 2010 22:19
by Stefan
TheQwerty wrote:

Code: Select all

	$prop = 1;
	while ($prop < 100) {
		$test = RegExReplace(Property("#$prop", "<curitem>"), "^[0-9]+:[0-9]+:[0-9]+$", ":::MATCH:::");
		if ($test Like ":::MATCH:::") { break; }
		$prop++;
	}
:D Clever :shock:
get the number of the length property on your system
XYplorer menu > Tools > Configuration > File Info Tips >> see the list on the right. For my XP 'Dauer' has # 21

Re: Calculate playing time of mp3's

Posted: 06 Jan 2010 22:22
by TheQwerty
Stefan wrote:XYplorer menu > Tools > Configuration > File Info Tips >> see the list on the right. For my XP 'Dauer' has # 21
Awww I completely forgot about that nice list by Don! :oops:
But yes it's #21 "Duration" here as well.

Though leaving it as is gives everyone a chance to significantly improve the performance. :lol: