As part of a larger project I needed to have a text file that contained the filenames and metadata for the contents of my Windows Media Player 9 library.

Below are two examples to show how it is done. The scripts are not identical, but are close enough for comparison.

BEFORE YOU BEGIN

Both scripts require an interop DLL to be created. You can create one with TLBIMP:

   tlbimp %SystemRoot%\system32\wmp.dll /out:Interop.WMPLib.dll

Place this DLL in the same folder as the script

POWERSHELL SAMPLE

#-----------


function get-script
{

   if($myInvocation.ScriptName) { $myInvocation.ScriptName }
   else { $myInvocation.MyCommand.Definition }
}


$script = get-script
$script_folder = split-path $script

$assemblyFile = join-path $script_folder "Interop.WMPLib.Dll"

if ( test-path $assemblyFile )
{
	write-host "Assembly Found" $assemblyFile 
}
else
{
	write-host "Could not find assembly" $assemblyFile 
	exit -1
}
$assemblyInfo = [System.Reflection.Assembly]::LoadFrom($assemblyFile)

if($assemblyInfo -eq $null) 
{
	write-host "Failed to load assembly" $assemblyFile 
	exit -1
}

$wmp = new-object WMPLib.WindowsMediaPlayerClass

write-host $wmp
$col = $wmp.getAll()
$total_in_collection = $col.count

"Items in collection $total_in_collection"
 
$attribs = ( "Title", "Artist", "WM/AlbumTitle", "AcquisitionTime", "Duration" , "Bitrate", "SourceURL" , "Is_Protected" , "MediaType" )

$exclude_exts = ( ".asx", ".wpl" )
$exclude_count = 0
$item_count = 0

for ($i=0; $i -lt $total_in_collection; $i++)
{
	
	if ( 0 -eq ( ($item_count) % 10) )
	{
		if ( $item_count >0) 
		{
			write-host ($item_count)	
		}
	}

	$item = $col.Item($i)

	$data = @{}
	foreach( $attr in $attribs)
	{
		$attr_value = $item.getItemInfo( $attr )
		$data[ $attr ] = $attr_value
	}
	write-output ""
	write-output $data
	$item_count ++
}

$processed_count = $total_in_collection-$exclude_count
write-host "Total: $total_in_collection"
write-host "processed: $item_count"
write-host "Excluded: $exclude_count"


#---------

 

IRONPYTHON SAMPLE

#-----------

import sys
import clr



output_filename = sys.argv[1]

import System
clr.AddReferenceToFile( "Interop.WMPLib.dll" )
from WMPLib import *

wmp = WindowsMediaPlayerClass()
col = wmp.getAll()
total_in_collection = col.count

print "Number of items:", col.count

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 ( (( (i+1) % 10) == 0)) :
        print str(i+1)+"/"+str(total_in_collection)

    # Get the current item
    item = col.get_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
    exclude = False
    if ( len(exclude_exts) ) :
        ext = System.IO.Path.GetExtension( data["SourceURL"] ).ToLower()
        if ( ext in exclude_exts ) :
            exclude = True

    if (exclude) :
        # skip this one
        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: [", ", ".join(exclude_exts), "]"


#---------

 

REFERENCE : THE FULL LIST OF ATTRIBUTES

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