Tuesday, 17 February 2009
With the advent of Windows PowerShell automating tasks in windows should now be a sinch for a .NET developer as PowerShell's scripting language is relatively easy to a C# programmer to pickup (trust me I've come down the path). One of the things I have a lot of is MP3 files from my extensive music collection and various podcats that I listen to, so I needed a way to manipulate these tags for example change some details such as adding missing information or even move the files into my music folderr structure.

A library that allowed me to do this easily was TagLib Sharp. It's avaliable from Novell and will work with .NET and Mono.

Because this is a .NET assembly you are free to use it in your C#, Visual Basic .NET. To use the library with any of thse languages simply add the assembly as a reference to your project and add the appropriate using or imports statement to the top of the class file that will use the library. However as PowerShell is my new best friend I'll show you how to use the library with PowerShell.

To use any assembly in your PowerShell script you can use the Assembly class in the System.Reflection namespace. The Assembly classes has the static LoadFile method that allows you to specify a location to load a dll from.


Once you have imported the assembly as shown above you can begin to use it to modify settings on you media files. For example the line below will create a File object that represents the specified physical media file.

$media = [TagLib.File]::Create( "$filePath" )

Once this has been done you simply access the various tags using $media.Tag.XXX. The script below will set the Title and Track information.

$media.Tag.Title = $matches["title"]
$media.Tag.Track = $trackNum.ToString()

Once you have set the tags to the values you want you save the changes by calling the Save method on the media file object.


One thing I did notice when using this library was that it failed to set the tags if they previously had no value so to get around this I manually set the tags to a character for sting values and 1 for numberic values suchas year and track number.

posted on Tuesday, 17 February 2009 15:16:51 (GMT Standard Time, UTC+00:00)  #    Add Comment | Comments [0]