Scripting Mp3 Tags through PowerShell
September 4th, 2009
1 comment
Structuring up you Music Library can be a rather time consuming task. You can however script this rather easily through PowerShell using the taglib-sharp library.
Let’s start off by downloading the Tag-Lib LibraryNext, We have to Load the Assembly from PowerShell in order to access the Classes.
PS > $TagLib = "C:\taglib\Libraries\taglib-sharp.dll" PS > [System.Reflection.Assembly]::LoadFile($TagLib) GAC Version Location --- ------- -------- False v2.0.50727 C:\taglib\Libraries\taglib-sharp.dllNow that we’ve loaded the dll, we need a music file that we want to set properties on, in this example I’m going to use a mp3 file. If we check out the properties on the mp3 file, we can see that nothing is set.
Let’s change this through PowerShell. We have to create a variable using the [Taglib.File] Class and use the filepath as an argument to the Create() method.
PS > $Media = [TagLib.File]::Create("C:\Music\My Song.mp3")
The object contains a couple of Properties.
PS > $Media Tag : TagLib.NonContainer.Tag Properties : TagLib.Properties TagTypesOnDisk : Id3v1, Id3v2 TagTypes : Id3v1, Id3v2 Name : C:\temp\My Song.mp3 MimeType : taglib/mp3 Tell : 0 Length : 0 InvariantStartPosition : 4608 InvariantEndPosition : 7109078 Mode : ClosedThe one that We’re interested in is the Tag property, which contains additional underlaying properties and methods. Let’s check it out:
PS > $Media.Tag
StartTag : TagLib.NonContainer.StartTag
EndTag : TagLib.NonContainer.EndTag
TagTypes : Id3v1, Id3v2
Tags : {Audiograbber 1.83.01, LAME d
v2.PrivateFrame TagLib.Id3v2.
Title :
Performers : {}
PerformersSort : {}
AlbumArtistsSort : {}
AlbumArtists : {}
Composers : {}
ComposersSort : {}
TitleSort :
AlbumSort :
Album :
Comment :
Genres : {}
Year : 0
Track : 0
TrackCount : 0
Disc : 0
DiscCount : 0
Lyrics :
Grouping :
BeatsPerMinute : 0
Conductor :
Copyright :
Pictures : {}
IsEmpty : True
Artists : {}
FirstArtist :
FirstAlbumArtist :
FirstAlbumArtistSort :
FirstPerformer :
FirstPerformerSort :
FirstComposerSort :
FirstComposer :
FirstGenre :
JoinedArtists :
JoinedAlbumArtists :
JoinedPerformers :
JoinedPerformersSort :
JoinedComposers :
JoinedGenres :
Let’s go ahead and set a couple of Values.
PS > $Media.Tag.Performers = "Goude" PS > $Media.Tag.AlbumArtists = "Goude" PS > $Media.Tag.Artists = "Goude" PS > $Media.Tag.Album = "My Album" PS > $Media.Tag.Genres = "Rock" PS > $Media.Tag.Track = "1" PS > $Media.Tag.Title = "My Song" PS > $Media.Tag.Year = "2009" PS > $Media.Tag.Comment = "PowerShell Demo" PS > $Media.Save()If we check out the file properties through Explorer we’ll see that the properties are set on the file.
So, editing one file is easy, but what if you want to edit a couple of thousand files..
When I did this on my Media Files i used the Folder and File Names as reference. The folder and file structure was set up accordingly:
C:\Music\Rock\Artist1 – Album\01 – First Song.mp3
C:\Music\Rock\Artist1 – Album\02 – Second Song.mp3
C:\Music\Rock\Artist2 – Album\01 – First Song.mp3
So with this information i could make a script that Used this information in order to set the Tags. The script uses the “Rock” folder name as Genre, Then it Splits the “Artist – Album” folder and sets Artist = Artist and Album = Album, finally it splits the file name “01 – First Song.mp3″ and sets Track = 01 and Title = First Song. I’ve also error handling in the script so that You can follow up on files that didn’t pass through the script. Click Here to download the Script Examples on Running the script:
PS > Set-Media.ps1 -Folder C:\MyMusic\Rock -LogFile C:\Log\SetMediaLog.txtNote! You can always modify the script to fit your own environment and folder structure.
[?]
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.
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.
Below is the Code USed in this Example
