Pages

Sunday, January 6, 2013

Writing Windows Media Play List (.wpl)

I was working with a lot of media files (.mp4) located on my local drive. The media files were categorized within nth-deep sub folders. Watching the media, I was using Window Media Player, and I knew Windows Media Player can read a XML playlist, so, I wrote a small WPF application that created a Windows Play List for each folder. Interesting, while writing, I noticed one line of code can write a complete Windows Play List XML file. The line of code is below:

var mediaListFile = MakeFilePath("index.wpl", di.FullName);

XDocument xDoc = new XDocument(new XElement("smil", new XElement("head", new XElement("title", di.Name)), new XElement("body", new XElement("seq", files.Select(f => new XElement("media", new XAttribute("src", f)))))));

 

xDoc.Save(mediaListFile);


Using recursion, I created a collection of media files, which I wrote out to an XML file on my local drive. After creating the XML file, I was able to double-click on the file and Windows Media Player would begin. The XML file was a Windows Media Play List.

The code is below:


private TreeNode CreateDirectoryNode(DirectoryInfo di)

{

    var directoryNode = new TreeNode(di.Name);

 

    foreach (var directory in di.GetDirectories())

    {

        directoryNode.Nodes.Add(CreateDirectoryNode(directory));

    }

 

    var files = GetFiles(di);

    foreach (var file in files)

    {

        directoryNode.Nodes.Add(new TreeNode(file));

    }

 

    if (files.Count() > 0)

    {

        var mediaListFile = MakeFilePath("index.wpl", di.FullName);

        XDocument xDoc = new XDocument(new XElement("smil", new XElement("head", new XElement("title", di.Name)), new XElement("body", new XElement("seq", files.Select(f => new XElement("media", new XAttribute("src", f)))))));

        xDoc.Save(mediaListFile);

    }

    return directoryNode;

}

 

private bool FilterFiles(string file)

{

    Regex FilterFile = new Regex(@"^[\w\-. ]+(\.mp4|\.wmv|\.mov|\.avi|\.mp3)$");

    return FilterFile.IsMatch(file);

}

 

private string StripFile(string oldFile, string path)

{

    Regex SpecialChars = new Regex(@"[^a-zA-Z0-9._ ]+");

    if (SpecialChars.IsMatch(oldFile))

    {

        var newFile = AddFilePrefix(SpecialChars.Replace(oldFile, "_"));

        var newFilePath = MakeFilePath(newFile, path);

 

        if (!File.Exists(newFilePath))

        {

            var oldFilePath = MakeFilePath(oldFile, path);

 

            File.Copy(oldFilePath, newFilePath);

        }

        oldFile = newFile;

    }

 

    return oldFile;

}

 

private List<string> GetFiles(DirectoryInfo di)

{

    List<string> files = new List<string>();

    string stripedFile;

    string path = di.FullName;

    foreach (var file in di.GetFiles())

    {

        if (FilterFiles(file.Name))

        {

            stripedFile = StripFile(file.Name, path);

            files.Add(stripedFile);

        }

    }

 

    return files;

}

This post is for the purpose of my notes only.


“I invented nothing new. I simply assembled the discoveries of other men behind whom were centuries of work. Had I worked fifty or ten or even five years before, I would have failed. So it is with every new thing. Progress happens when all the factors that make for it are ready and then it is inevitable. To teach that a comparatively few men are responsible for the greatest forward steps of mankind is the worst sort of nonsense.”
Henry Ford