Home > Media > Scripting Mp3 Tags through PowerShell

Scripting Mp3 Tags through PowerShell

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 Library
Next, 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.dll

Now 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.

taglib01

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                   : Closed

The 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.

taglib02

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.txt

Note! You can always modify the script to fit your own environment and folder structure.

Rating 3.00 out of 5
[?]
Categories: Media Tags:
  1. December 29th, 2009 at 20:57 | #1

    Just what I was looking for.
    As I have quite a large library,
    and the new Logitech Squeezebox uses the MP3 tags extensively.
    Also in my car the MP3 player uses tags rather than filenames,
    to the order of the stuff played is often not ok which is annoying with books in MP3.

    So, I’ll be taking your examples for a spin.
    Thanks.

  1. No trackbacks yet.

Spam Protection by WP-SpamFree