Here is a bit of a change from the usual datacenter type posts. A nameless friend of mine recently plugged her Ipod into a different PC that was set to auto-sync. It appeared to erase all of her MP3s from her Ipod without prompting or intervention. I am not familiar with iTunes, iPods, or any Apple products, but a quick search suggested this was a common occurrence. There are even a few software packages out there to recover the information, as the files don’t actually get deleted. It seems just some meta data gets removed. After running one such software package, all the MP3s were recovered, but some of them had weird file names or were missing various tag attributes. I tried using common MP3 tag editing software, such as MP3Tag, but found that I needed some logic to get everything squared away. I wrote a quick and dirty powershell script to standardize the filenames and tag information, based on which attributes were still present. Script is below.
As I continued to look through these files, I’d find different scenarios in the files that I needed to account for in the script. For example, a question mark (?) is OK to have in an MP3 tag, but not in a filename. As I went, I added these scenarios. The result is a procedural script to get the job done.
Now, you may not be solving the exact same problem that I was, but in this script, we show how to load an external .DLL in powershell, how to get and set MP3 tags with it, how to rename files, how to strip out illegal file characters and more! Lots of good stuff in here. It should make a pretty good starting point for any MP3 organization issue you need to sort out.
First, you”ll need the Taglib mp3 editing library. You can download that here. Just grab the .dll file and put it in the same directory that you run your powershell script from.
Next, grab the script from below.
Finally, at the top of the script, add the path to your MP3 files that you need to edit. After you run this script, your MP3 files will get renamed like so: “Artist Name-Song Title.mp3”. The tags will also be updated to match.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
######################################### # MP3 Tag Update Script # # Requires Taglib library # # www.rickgouin.com # 9/9/2015 ######################################### # Enter the path to the file you need to update. Include a trailing slash. $path = “c:\MP3Files\here\” # Load the assembly that will handle the MP3 tagging. [Reflection.Assembly]::LoadFrom(“taglib.dll”) # Get a list of files in your path. Skip directories. $files = Get-ChildItem -Path $path | Where-Object { (-not $_.PSIsContainer) } # Loop through the files foreach ($filename in $files){ # Load up the MP3 file. $media = [TagLib.File]::Create(($path+$filename)) # Load up the tags we know $albumartists = [string]$media.Tag.AlbumArtists $title = $media.Tag.Title $artists = [string]$media.Tag.Artists $extension = $filename.Extension # A few files had no title. Lets just save them with an artist name if([string]::IsNullOrEmpty($title)) { $title = “missing title” $media.Tag.Title = $title } # If the artists tag has info in it, use that, then reset albumartists tag to match if ($artists) { $name = $artists+”-“+$title.Trim()+$extension $media.Tag.AlbumArtists = $artists } # If the artists tag is empty, use the albumartists field, and set artists to match else { $name = $albumartists+”-“+$title.Trim()+$extension $media.Tag.Artists = $albumartists } # Save the tag changes back $media.Save() #remove any carriage returns in what will be the new filename $name = [string]$name -replace “`t|`n|`r”,”” #remove illegal characters, replace with a hyphen [System.IO.Path]::GetInvalidFileNameChars() | % {$name = $name.replace($_,’ ‘)} # There could be duplicate MP3 files with this name, so check if the new filename already exists If (Test-Path $path$name) { [int]$i = 1 #if the file already exists, re-name it with an incrementing value after it, for example: Artist-Song Title-2.mp3 While (Test-Path $path$newname) { $newname = $name $justname = [System.IO.Path]::GetFileNameWithoutExtension($path+$newname) $newname = $justname+”-“+$i+$extension $i++ } $name = $newname } #rename the file per those tags Rename-Item $path$filename $path$name } |
Lets give credit where credit is due! I got the basics for this script from this blog post. Was this script useful to you? Have any other scenarios you would like to see handled by the script? Any issues running it? Let me know in the comments below.
** Updated 02/03/2022 with new DLL name and project location **
Categories: Scripting Tech Soup
You need admin properties to “LoadFrom”…
😉
Just starting to use PS. Exactly what I was looking for. Thank you.
Will this work for MP4 tags?
TagLib is now at:
https://taglib.org/
(Seems to support MP4 too)
Compiled for Windows at:
https://sourceforge.net/projects/taglib-winport/files/latest/download
The DLL name seems to have changed in the latest iteration. It’s now simply taglib.dll – change line 14 to match…
Hi Rick,
I followed your instructions step by step:
“First, you”ll need the Taglib mp3 editing library. You can download that here. Just grab the .dll file and put it in the same directory that you run your powershell script from.
Next, grab the script from below.”
Powershell complained:
Exception callin “LoadFrom” with 1 Argument(s): “File or Assembly “file:///C:\temp\taglib\taglib.dll” or a dependency was not found. In Modul was a Assemblymanifest expected.”
In Zeile:14 Zeichen:1
+ [Reflection.Assembly]::LoadFrom(“C:\temp\taglib\taglib.dll”)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : BadImageFormatException
What’s my fault?
Thanks a lot
Thank you for an interesting article and potentially useful script, Rick.
When I tried I ran into the same problem as Joerg. The dll file I found under the link you gave was
taglib.dll 290 816 2005.07.22 00:05 -a–
which I unpacked from
d:\Data\Audio\!Data_Management\PShell\taglib-1.3.1-bin.zip 823 108 2023.11.30 17:44 -a–
The zip also contained a debug version (taglib_d.dll) which I tried using, to no avail.
@Joerg: Did you get any further?