Enumerating Windows Media Player Library Contents
From IronPython Cookbook
Adapted from this blog entry.
This is an example of interoperating with Windows Media Player, using the IWMPPlaylist Interface.
Run tlbimp. See Interop introduction for more information:
tlbimp %SystemRoot%\system32\wmp.dll /out:Interop.WMPLib.dll
Or use ProgID "MediaPlayer.MediaPlayer.1".
The following script (when run), generates a text file containing details of your Windows Media Player library. You pass the name of the output file as the command line argument:
import sys
import clr
output_filename = sys.argv[1]
import System
clr.AddReferenceToFile( "Interop.WMPLib.dll" )
from Interop.WMPLib import WindowsMediaPlayerClass
wmp = WindowsMediaPlayerClass()
col = wmp.getAll()
total_in_collection = col.count
print "Number of items:", total_in_collection
attribs = (
"Title", "Artist", "WM/AlbumTitle",
"AcquisitionTime", "Duration" , "Bitrate",
"SourceURL" , "Is_Protected" , "MediaType"
)
exclude_exts = ['.asx','.wpl']
exclude_count = 0
fp = file(output_filename,"w")
#Loop through every item in the collection
for i in xrange(total_in_collection):
# Print a header every 10 items
if not ((i+1) % 10):
print str(i+1) + "/" + str(total_in_collection)
# Get the current item
item = col.Item[i]
#collect the data about the item
data = {}
for attr in attribs :
attrib_value = item.getItemInfo(attr)
data[attr] = attrib_value
#identify if the item should be excluded
if Path.GetExtension(data["SourceURL"]).lower() in exclude_exts:
exclude_count += 1
else:
#emit the data
print >> fp
for attr in attribs:
print >> fp, attr + ": ", data[attr]
fp.close()
print "Done."
print "Total:", total_in_collection
print "Handled:", total_in_collection - exclude_count
print "Skipped:", exclude_count
print "Excluded Extensions: %s " % (', '.join(exclude_exts),)
Currently this is writing a file that contains odd characters - it would be nice to work out which attribute they are coming from (is it just non-ASCII that needs encoding?). Other than that, it does what it says on the tin...
Back to Contents.

