Home > Media > Windows Media Player, MP3 and PowerShell

Windows Media Player, MP3 and PowerShell

Let’s have some fun with MediaPlayer and MP3 Files. In this example, we’ll look at setting Attributes on MP3 files. First off, Let’s look at the files we want to set Attributes on:


PS > ls | select Name


Name
----
01 - The Wicker Man.mp3
02 - Ghost Of The Navigator.mp3
03 - Brave New World.mp3
04 - Blood Brothers.mp3
05 - The Mercenary.mp3
06 - Dream Of Mirrors.mp3
07 - The Fallen Angel.mp3
08 - The Nomad.mp3
09 - Out Of The Silent Planet.mp3
10 - The Thin Line Between Love And Hate.mp3

If we look at the files through Explorer, we see that they are missing their attributes. This is rather annoying if you copy them to your MP3 player and try to sort on Artist or Album. Since they don’t have any Attributes set, they will be classed as unknown. Let’s change this through PowerShell.

mediaplayer-01

Let’s start with One file. First we need to Get information about the Item. We can do this through the Get-Item CmdLet. Get-Item Displays some, but not all information we wnat to access. To solve this, we will use the Object with the WMPlayer ComObject.


PS > $TheWickerMan = Get-Item '01 - The Wicker Man.mp3'
PS > $MediaPlayer = New-Object -Com WMPlayer.OCX
PS > $GetAttributes = $MediaPlayer.mediaCollection.add($TheWickerMan)
PS > $GetAttributes


sourceURL         : C:UsersnigoMusicIron Maiden - Brave New World�1 - The Wicker Man.mp3
name              : 01 - The Wicker Man
imageSourceWidth  : 0
imageSourceHeight : 0
markerCount       : 0
duration          : 275,539
durationString    : 04:36
attributeCount    : 98
Error             :

This Shows Some, but not all available Attributes, to get a complete List of available Attributes we can Loop through the AttributeNames with a for Loop. We use the AttributesCount to get the amount of Attributes. Note that the for Loop starts at Zero.


PS > $Attributes = $GetAttributes.attributeCount

PS > for ($i = 0; $i -lt $Attributes; $i++) { $GetAttributes.getAttributeName($i) }

Let’s get on to business and start setting Atrributes. We do this with a little help from MediaPlayer.


PS > $TheWickerMan = Get-Item '01 - The Wicker Man.mp3'
PS > $MediaPlayer = New-Object -Com WMPlayer.OCX

PS > $SetItemInfo = $MediaPlayer.mediaCollection.add($TheWickerMan)
PS > $SetItemInfo.setItemInfo('Title', "The Wicker Man")
PS > $SetItemInfo.setItemInfo('Artist', "Iron Maiden")
PS > $SetItemInfo.setItemInfo('Composer', "Iron Maiden")
PS > $SetItemInfo.setItemInfo('Album', "Brave New World")
PS > $SetItemInfo.setItemInfo("WM/TrackNumber", "01")
PS > $SetItemInfo.setItemInfo("WM/Genre", "Heavy Metal")
PS > $SetItemInfo.setItemInfo("UserRating", 100)

If we look at the File now, the Attributes are updated.

mediaplayer-02

Doing this on every single file can be a rather time consuming job. so here’s a function that does it for us. Note that in this example I use the replace operator based on the naming of my files. If you have a different way of naming your music files you might want to modify the function. I added the Start-Sleep CmdLet because i had some issues with the update, if you experience issues you can increase the Start-Sleep Seconds.


PS > function Set-MusicProperties {

param (
	[string]$Artist = $(throw "Enter Artist Name"),
	[string]$Composer = $(throw "Enter Composer"),
	[string]$Album = $(throw "Enter Album Name"),
	[string]$Genre = $(throw "Enter Genre"),
	[int]$Rating = $(throw "Enter Rating 0-100")
)

	$CurrentPath = (Get-Location).Path
	$Items = Get-ChildItem -path $CurrentPath | Select Name

	$MediaPlayer = New-Object -Com WMPlayer.OCX

	$Items | ForEach-Object {

		Write-Host "Waiting For Files To Update"
		Start-Sleep -s 3

		$AddItem = Get-Item $_.Name

		$TrackNumber = $_.Name -replace " - .*",""
		$Title = $_.Name -replace ".* - ",""

		$SetItemInfo = $MediaPlayer.mediaCollection.add($AddItem)
		$SetItemInfo.setItemInfo('Title', $Title)
		$SetItemInfo.setItemInfo('Artist', $Artist)
		$SetItemInfo.setItemInfo('Composer', $Composer)
		$SetItemInfo.setItemInfo('Album', $Album)
		$SetItemInfo.setItemInfo("WM/TrackNumber", $TrackNumber)
		$SetItemInfo.setItemInfo("WM/Genre", $Genre)
		$SetItemInfo.setItemInfo("UserRating", $Rating)

		Write-Host "$Title Updated"
	}
}

Now all we got to do is set the location to the folder containing the files and call on the function and providing the arguments we wnat to set.


PS > Set-MusicProperties "Iron Maiden" "Iron Maiden" "Brave New World" "Heavy Metal" 100

Open Explorer and your MP3 Files Attributes should be updated

mediaplayer-03

Below is the Code USed in this Example


$TheWickerMan = Get-Item '01 - The Wicker Man.mp3'
$MediaPlayer = New-Object -Com WMPlayer.OCX
$GetAttributes = $MediaPlayer.mediaCollection.add($TheWickerMan)

$Attributes = $GetAttributes.attributeCount

for ($i = 0; $i -lt $Attributes; $i++) { $GetAttributes.getAttributeName($i) }

$TheWickerMan = Get-Item '01 - The Wicker Man.mp3'
$MediaPlayer = New-Object -Com WMPlayer.OCX

$SetItemInfo = $MediaPlayer.mediaCollection.add($TheWickerMan)
$SetItemInfo.setItemInfo('Title', "The Wicker Man")
$SetItemInfo.setItemInfo('Artist', "Iron Maiden")
$SetItemInfo.setItemInfo('Composer', "Iron Maiden")
$SetItemInfo.setItemInfo('Album', "Brave New World")
$SetItemInfo.setItemInfo("WM/TrackNumber", "01")
$SetItemInfo.setItemInfo("WM/Genre", "Heavy Metal")
$SetItemInfo.setItemInfo("UserRating", 100)

function Set-MusicProperties {

param (
	[string]$Artist = $(throw "Enter Artist Name"),
	[string]$Composer = $(throw "Enter Composer"),
	[string]$Album = $(throw "Enter Album Name"),
	[string]$Genre = $(throw "Enter Genre"),
	[int]$Rating = $(throw "Enter Rating 0-100")
)

	$CurrentPath = (Get-Location).Path
	$Items = Get-ChildItem -path $CurrentPath | Select Name

	$MediaPlayer = New-Object -Com WMPlayer.OCX

	$Items | ForEach-Object {

		Write-Host "Waiting For Files To Update"
		Start-Sleep -s 3

		$AddItem = Get-Item $_.Name

		$TrackNumber = $_.Name -replace " - .*",""
		$Title = $_.Name -replace ".* - ",""

		$SetItemInfo = $MediaPlayer.mediaCollection.add($AddItem)
		$SetItemInfo.setItemInfo('Title', $Title)
		$SetItemInfo.setItemInfo('Artist', $Artist)
		$SetItemInfo.setItemInfo('Composer', $Composer)
		$SetItemInfo.setItemInfo('Album', $Album)
		$SetItemInfo.setItemInfo("WM/TrackNumber", $TrackNumber)
		$SetItemInfo.setItemInfo("WM/Genre", $Genre)
		$SetItemInfo.setItemInfo("UserRating", $Rating)

		Write-Host "$Title Updated"
	}
}

Set-MusicProperties "Iron Maiden" "Iron Maiden" "Brave New World" "Heavy Metal" 100

Rating 3.00 out of 5
[?]
Categories: Media Tags:
  1. January 20th, 2010 at 18:44 | #1

    I adore the precious learning you presentation in your post. I will bookmark your blog and have my son check up here frequency. I am quite sure they will gain a lots of new stuff here than anybody else!

  1. No trackbacks yet.

Spam Protection by WP-SpamFree