Fixing up my music library with Powershell

Background

I prefer to encode my music using the OGG format. The majority of my music (still, just) actually comes from my CDs, and I rip them into FLAC. To compress them a bit more for my media player, I convert the FLAC files to OGG, quality-level 7. One day I should really sit down with MP3 again and do a listen test – when MP3s all used to be encoded with a constant bitrate, they weren’t very efficient with size vs quality.

Anyway, one of the drawbacks of encoding an OGG file is that it doesn’t have the ability to embed the album art into the music tags. Or, it can theoretically, but there is no actual implementation for doing so (c’mon, people). While Media Monkey (which I use to manage my music library, and do the ripping and tagging) can cram it in somehow for its own purposes (probably using a custom field – I haven’t bothered looking), it doesn’t work on my portable media player, a Cowon J3. I should actually experiment with the FLAC files – which my media player also handles – since you can tag each track with art too. Another day. In any case, I would like to look at the album art while tracks are playing, and at the moment I can’t (unless it’s an MP3 with embedded art).

What my media player does do is display an image if there is a file called “cover.jpg” in the album folder. Following the usual convention, I rip my albums so there is a separate folder for each one. There’s also a way to “embed” images for single  music files that might be in a “miscellaneous” folder of random non-album music – rename the JPG to the same name as the music file. For example, for “Skatalites-Addis Ababa.ogg”, ensure the associated album art image is called “Skatalites-Addis Ababa.jpg”.

The problem

Media Monkey will helpfully download album art for you (from sources like Google Images, etc) and plonk it into the associated album folder. The trouble is, these files are named “Awesome Album Name.jpg” rather than “cover.jpg”. My media player won’t associate files called “OK Computer.jpg” with the currently-playing track, alas.

All my music is in a folder structure that is like Music > Artist > Album1[,Album2][,Album3]. Naturally, there are hundreds of subfolders and “album.jpgs” in each folder containing music. How to convert all these random names into one consistent name…

Solution

If anyone still needs an excuse to upgrade from XP to Windows 7 (or up), Powershell is it. (Sh, I know you can download it for XP – the OS really is now too crusty around the edges). Changing all those random jpg names to “cover.jpg” is a one-liner. Yay!

Assuming you’re running the script from the top of your music folder tree:

PS C:\Music> Get-ChildItem -Filter "*.jpg" -Recurse | Rename-Item -NewName {$_.name -replace $_.name,'cover.jpg'}

I was really unsure that $_.name (the variable holding the file name of each *.jpg file the Get-ChildItem had found) would work as something that could just be substituted, but there you go. I took the precaution of doing a -whatif, and there was lots of lovely output like the following:

What if: Performing operation "Rename File" on Target "Item: 
C:\music\The Bombay Royale\PhoneBaje Na Remix 12\PhoneBaje.jpg Destination: 
C:\music\The Bombay Royale\Phone Baje Na Remix 12\cover.jpg".
What if: Performing operation "Rename File" on Target "Item: 
C:\music\The Upbeats\Big Skeleton\Big Skeleton.jpg Destination: 
C:\music\The Upbeats\Big Skeleton\cover.jpg"

Executing the actual command completed silently, and all the files were renamed within about 5 seconds.

The Scripting Guys, where I swiped the guts from, also explain how to do substring substitution in file names using the ”-match” operator.

I like it when it takes me almost as little time to figure out a quick command than it does to actually execute it. Doesn’t happen often!

Advertisements
This entry was posted in Non-work and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s