As most of you know, I do a wildly successfully weekly SharePoint Netcast. Not only am I the star, I’m also the writer, director, producer, and janitor. As the years have gone by I’ve tried to improve the Netcast, but that almost always means I spend more time producing it. Because of that I’ve tried automate as much of the production as I can. One piece of that is putting all the tags on the MP3 files. The program I use to produce my Netcast, Camtasia, is fantastic, absolutely fantastic. But it doesn’t tag the MP3 files it produces. Windows will let you manually tag the text fields of an MP3 file in Explorer, but you can’t add cover art that way. I’m way too pretty to not be on the cover art of my MP3. So I have had to import the MP3 into Windows Media Player and add the cover art that way. Awfully labor intensive. I’m way too lazy for all that. Time to bring out the big guns, PowerShell.
I spent some time trying to find a way to edit MP3 tags in PowerShell natively. I was surprised that I couldn’t find one. Fortunately PowerShell can wedge its way into all kinds of places. I was able to find a library, Taglib, that did the job. It allowed me to easily add both the text and picture information I needed. Here’s roughly the code I used to tag the MP3 for Netcast 185.
# Load the assembly. I used a relative path so I could off using the Resolve-Path cmdlet
[Reflection.Assembly]::LoadFrom( (Resolve-Path "..\Common\taglib-sharp.dll"))
# Load up the MP3 file. Again, I used a relative path, but an absolute path works too
$media = [TagLib.File]::Create((resolve-path ".\Netcast 185 - Growing Old with Todd.mp3"))
# set the tags
$media.Tag.Album = "Todd Klindt's SharePoint Netcast"
$media.Tag.Year = "2014"
$media.Tag.Title = "Netcast 185 - Growing Old with Todd"
$media.Tag.Track = "185"
$media.Tag.AlbumArtists = "Todd Klindt"
$media.Tag.Comment = "http://www.toddklindt.com/blog"
# Load up the picture and set it
$pic = [taglib.picture]::createfrompath("c:\Dropbox\Netcasts\Todd Netcast 1 - 480.jpg")
$media.Tag.Pictures = $pic
# Save the file back
$media.Save()
I’m only setting a few properties, but there are many more. To get the full list of properties you can get and set, issue this command:
$media.tag | Get-Member
Most, but not all, of the properties can be written to. Here are a couple of properties to compare:
Grouping Property string Grouping {get;set;}
IsEmpty Property bool IsEmpty {get;}
Properties with only a “get” in their definition, like “IsEmpty” are read-only. You can only get them. Properties that have both “get” and “set,” like “Grouping” are read and write. That’s not something that’s specific to taglib or MP3 files, that’s a PowerShell thing.
Once you’ve set whichever properties you want to set, use the .Save() method to write those changes back to your MP3 file. I’m using this weekly to tag a single file, but this could just as easily be used to bulk tag (or retag) MP3s, or move MP3s around based on information in their properties.
Enjoy. Leave a comment below if you use this in an interesting way.
tk
ShortURL: http://www.toddklindt.com/MP3TagswithPowerShell