« The Humor of ReSharper 7 | Main | An RGB Atlas of Colors - Just a page after page of sweet, glorious RGB »
Friday
Sep072012

Exporting Windows Media Player Metadata to a CSV file  

I've written about this topic before except at that time dealing with IronPython and PowerShell. Now I'll cover it in C#. (The reason I needed this today is that I decided to use my Media Player library as a convenient source for sample data for another project I am working on.)

If you need the binaries published, just let me know.

The code was tested with Visual Studio 2012 and .NET 4.0 and Windows 8 RTM. Keep in mind you'll need to get as a reference to WMPLib which is found at C:\Windows\System32\wmp.dll.

 

using System.Collections.Generic;

namespace ExportWMPMetdataToCSV
{
    class Program
    {
        static void Main(string[] args)
        {
            string output_filename = args[0];
            // Add a Reference to WMPLib at c:\windows\system32\wmp.dll
            // for VS2010 and VS2010 - make sure that for the WMPLib 
            //    reference you set Embed Interop Types to FALSE
            var normalized_names = new Dictionary();
            foreach (var attr in all_attributes)
            {
                string name = attr.Replace("/", "_");
                normalized_names[attr] = name;
            }

            var wmp = new WMPLib.WindowsMediaPlayerClass();
            var item_collection = wmp.getAll();

            using (var s = System.IO.File.Create(output_filename))
            {
                // using the StreamWriter like this will cause the UTF-8 BOM to be written first
                // this makes it easier to work with in Excel

                using (var w = new System.IO.StreamWriter(s, System.Text.Encoding.UTF8))
                {
                    // Write CSV Header
                    for (int i = 0; i < all_attributes.length; i++)
                    {
                        if (i > 0)
                        {
                            w.Write(sep);
                        }
                        w.Write(Escape(normalized_names[all_attributes[i]]));
                    }
                    w.WriteLine();

                    for (int i = 0; i < item_collection.count; i++)
                    {
                        system.console.writeline("WMP Item #: {0}",i+1);
                        // write a csv line for each item in the collection
                        var item ="" item_collection.get_item(i);

                        for (int j ="" 0; j < all_attributes.length; j++)
                        {
                            var attr_value ="" item.getiteminfo(all_attributes[j]) ?? "";
                            if (j > 0)
                            {
                                w.Write(sep);
                            }
                            w.Write(Escape(attr_value));
                        }
                        w.WriteLine();
                    }
                    w.Flush();
                }
            }
            System.Console.WriteLine("DONE");

        }

        public static string Escape(string s)
        {
            if (s.Contains(QUOTE))
                s = s.Replace(QUOTE, ESCAPED_QUOTE);

            if (s.IndexOfAny(CHARACTERS_THAT_MUST_BE_QUOTED) > -1)
                s = QUOTE + s + QUOTE;

            return s;
        }

        private const string sep = ",";
        private const string QUOTE = "\"";
        private const string ESCAPED_QUOTE = "\"\"";
        private static char[] CHARACTERS_THAT_MUST_BE_QUOTED = { ',', '"', '\n' };

        private static string[] all_attributes = new string[]
                                               {
                                                   "AcquisitionTime",
                                                   "AlbumID",
                                                   "AlbumIDAlbumArtist",
                                                   "Author",
                                                   "AverageLevel",
                                                   "Bitrate",
                                                   "BuyNow",
                                                   "BuyTickets",
                                                   "Copyright",
                                                   "CurrentBitrate",
                                                   "Duration",
                                                   "FileSize",
                                                   "FileType",
                                                   "Is_Protected",
                                                   "IsVBR",
                                                   "MediaType",
                                                   "MoreInfo",
                                                   "PeakValue",
                                                   "ProviderLogoURL",
                                                   "ProviderURL",
                                                   "RecordingTime",
                                                   "ReleaseDate",
                                                   "RequestState",
                                                   "SourceURL",
                                                   "SyncState",
                                                   "Title",
                                                   "TrackingID",
                                                   "UserCustom1",
                                                   "UserCustom2",
                                                   "UserEffectiveRating",
                                                   "UserLastPlayedTime",
                                                   "UserPlayCount",
                                                   "UserPlaycountAfternoon",
                                                   "UserPlaycountEvening",
                                                   "UserPlaycountMorning",
                                                   "UserPlaycountNight",
                                                   "UserPlaycountWeekday",
                                                   "UserPlaycountWeekend",
                                                   "UserRating",
                                                   "UserServiceRating",
                                                   "WM/AlbumArtist",
                                                   "WM/AlbumTitle",
                                                   "WM/Category",
                                                   "WM/Composer",
                                                   "WM/Conductor",
                                                   "WM/ContentDistributor",
                                                   "WM/ContentGroupDescription",
                                                   "WM/EncodingTime",
                                                   "WM/Genre",
                                                   "WM/GenreID",
                                                   "WM/InitialKey",
                                                   "WM/Language",
                                                   "WM/Lyrics",
                                                   "WM/MCDI",
                                                   "WM/MediaClassPrimaryID",
                                                   "WM/MediaClassSecondaryID",
                                                   "WM/Mood",
                                                   "WM/ParentalRating",
                                                   "WM/Period",
                                                   "WM/ProtectionType",
                                                   "WM/Provider",
                                                   "WM/ProviderRating",
                                                   "WM/ProviderStyle",
                                                   "WM/Publisher",
                                                   "WM/SubscriptionContentID",
                                                   "WM/SubTitle",
                                                   "WM/TrackNumber",
                                                   "WM/UniqueFileIdentifier",
                                                   "WM/WMCollectionGroupID",
                                                   "WM/WMCollectionID",
                                                   "WM/WMContentID",
                                                   "WM/Writer"
                                               };
    }
}

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>