Scroll To Top

Use Powershell to Edit MP3 Tags

Posted in Scripting, Tech Soup2 years ago • Written by Rick Gouin1 Comment

PowerShell LogoHere 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.

#########################################
# 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-sharp.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.

TAGS:

1 Comment so far. Feel free to join this conversation.

  1. goyete July 14, 2016 at 4:57 am - Reply

    You need admin properties to “LoadFrom”…
    😉

Leave A Response