#!C:/perl/bin -w # buildasx.pl # Build a (Windows) .asx playlist of .mp3s (Caveat: PRESUMED encoded at 128Kbps bitrate) # .mp3 ONLY: see TODO # subdir selected must not be more than 4 deep # ww 2007-05-07 # TODO #################################################################################### # timing calculation is imprecise (seconds are approx) , but so far, appears unimportant # Add support for .wav etc. # add UI to let user select dir and asx filename, defaulting to CWD and CWD's name (replacing hardcoded name at 20) # and maybe, replace regex for ident of dir with something better # END TODO as of 2007-05-07 ########################################################################################### ##### Get the output of W32's dir cmd to @list $input = qx{dir} || print STDERR "Couldn't do qx{dir} \/n: $!"; @list = split (/\n/, $input); ##### Open output file (title.asx) and print headers open (ASX, ">title.asx") || die "Can't open title.asx for write: $!"; print ASX '' . "\n" . '' . "\n" . '' . "\n\n"; #### Now, use info from the dir at line 15 and capture those elements needed for .asx for $list(@list) { if ($list =~ / ([A-X]: # BEGIN ($dir) CAPTURE: Drive letter \\[\s,A-Z,a-z,-,_,0-9]* # first child \\[\s,A-Z,a-z,-,_,0-9]* # second child \\[\s,A-Z,a-z,-,_,0-9]* # third child \\[\s,A-Z,a-z,-,_,0-9]*) # fourth child, END CAPTURE, END of conditional test /x ) { # extended, END of conditional test $dir = $1; next; } if ($list =~ / (\d{2}\/\d{2}\/\d{4} # CAPTURE_1 (create_date) \s+ # space(s) \d{2}:\d{2}[ap]) # create_time END_CAPTURE \s+ # space(s) (\d+,\d{3},\d{3}) # CAPTURE_2 (file_size) END_CAPTURE \s+ # space(s) ([A-Z,a-z,0-9] # CAPTURE_3: (file_name) first char of name [^.]* # anything not a literal period \. # literal (name-type separator) mp3) # title END_CAPTURE /xi ) { # extended, CaseInsens, END of conditional test $file_name = $3; $file_size = $2; $create_date = $1; #### Convert filesize to (rough) duration in seconds and thence to minutes:seconds:(kluded)decimals : if ($file_size) { $file_size =~ s/,//g; # remove commas, altho using "dir /-C" would save us this cleanup $sec_dec = $file_size/16000; # in seconds though something 16,042..16,080 is more nearly precise $min_dec = ($sec_dec/60); # minutes and decimal minutes $mins = int ( $min_dec); # drop the decimal fraction if (length($mins) == 1) { # and prepend a zero if mins < 10 $mins = "0" . $mins; } $secs = $sec_dec % 60; # seconds NOT precise if (length($secs) == 1) { # and prepend a zero if secs < 10 $secs = "0" . $secs; } $duration = "00:$mins" . ":" . "$secs" . ".999"; # not expecting any selection to exceed 59 min 59.999 sec # the ".999" is a kludge to offset possible modulus truncation } else { $duration = "xx:xx:xx.000"; } #### now print specific entry: print ASX "\n \n"; print ASX " $file_name \n"; print ASX '' . "\n\n" . '' . "\n\n" . '' . "\n\n" . '' . "\n\n" . '' . "\n\n" . '' . "\n\n" . '' . "\n\n" . '' . "\n\n" . '' . "\n\n" . '' . "\n" . '' . "\n\n"; } # END OF conditional_action } # END OF for #### Print close tag for .asx and close file print ASX ''; close ASX; exit;