# Create windows media play playlists in ASX format.
# Takes one or more directory names as command line arguments, finds all .wma files
# and write a file called DIR.asx back into this directory, where DIR is the
# last element of the directory name. The files are listed in sorted order.
#
# Copyright (c) David Elworthy 2004, but frankly you can do whatever you like with it.
#
# I wrote this script for the following reason. You can easily copy an audio
# CD into .wma files using Windows Media Player, but if you then take the
# files and drag&drop them back into WMP they do not play in the correct
# order. This is more than a little daft, as WMP even puts a numeric prefix on
# the file names. The script creates an ASX file which has the files in the
# correct order.
#
# I did not find much documentation on the ASX file format, so what you get is
# based on looking at a couple of examples.
use strict;
use File::Basename;
my $header =
"\n";
my $trailer =
"\n";
foreach my $dir (@ARGV)
{
# Check this really is a directory and take apart the name
# Do some fixing up if it looks like it came from cygwin
if (!(-d $dir))
{
print STDERR "$dir is not a directory\n";
next;
}
$dir =~ s!/$!!;
$dir =~ s!^(/cygdrive)?/([a-z])/!$2:/!i;
$dir =~ s!/!\\!g;
$dir =~ m!(.+\\)?(.+)!;
my $last = $2;
my $listname = $dir."\\".$last.".asx";
# Get a list of all .wma files
if (!opendir(DIR, $dir))
{
print STDERR "Cannot read file list for $dir\n";
}
my @files;
foreach my $file (readdir(DIR))
{
if ($file =~ /\.wma$/i)
{
push @files, $file;
}
}
closedir DIR;
if (!open(O, ">$listname"))
{
print STDERR "Could not open $listname\n";
next;
}
print O $header;
# Use last part of directory name as the title
print O "
$last\n";
# Sort file names and write them
foreach my $file (sort @files)
{
# Use just the file name on the assumption we are writing the playlist into
# the same directory as the audio files.
print O " \n";
print O " [\n";
print O " ]\n";
}
print O $trailer;
close O;
}