So everyone’s got a bunch of MP3 files stashed away in a folder. Chances are, many of them don’t have proper filenames, and look something like:
C:\Music\Unknown Artist\Unknown Album\QSCD.mp3
However, there is hope – especially for aspiring programmers. Your mp3 files may already have the track name, artist and album details stored in ID3 tags – if you’re able to see this information in iTunes/Windows Media Player/Winamp.
This problem started bugging me a while ago while I was switching from iTunes to Winamp, and my appetite for a suitable Computer Science activity growled eagerly. I thought of trying a Java-based solution – it went somewhat like this:
- Find a free (& maybe open source) Java ID3 reader package
- Learn how to enumerate files in a directory and pick out MP3s
- String this knowledge together
The solution was pretty simple:
- The JID3.jar package has support for reading and writing to ID3 tags – both v1 and v2 and it’s free-ish licence suted me just fine. They had a couple of code samples on the site too.
- I remembered seeing an example of this a couple of source code files ago, and googled. Apparently it’s a simple matter of:
File folder = new File("C:\Music\"); File[] subFiles = folder.listFiles(); - The final utility turned out to be pretty simple. At the heart of the code was this tag-reading snippet:
MP3File mp3file = new MP3File(file);
ID3Tag[] id3Tags = mp3file.getTags();
String title = null;
for (ID3Tag tag : id3Tags)
{
if (tag instanceof ID3V1_0Tag)
{
System.out.println("ID3v1: " + file.getName());
ID3V1_0Tag tag1 = (ID3V1_0Tag) tag;
if ( (title = tag1.getTitle()) != null)
return title;
} else if (tag instanceof ID3V2_3_0Tag)
{
System.out.println("ID3v2_3: " + file.getName());
ID3V2_3_0Tag tag23 = (ID3V2_3_0Tag) tag;
if ( (title = tag23.getTitle()) != null)
return title;
}
}
But that was far from the end of the project. I wanted this thing to run through my entire music folder and apply itself to every “.mp3″ file. As a bonus, it needed to remove track numbers from files like “01 Sage of Lamberene.mp3″. A most pleasurable evening later, it was finished. “public class MP3TagFileRenamer” even featured a recursive method to tackle subfolders within subfolders.
For those who’d like to tinker with it, here’s the source code. A bit messy, but legible and modular nonetheless. Legal Notice: Play all you want with it – but I am not liable for any (potential) damages incurred.
Future updates? The next step would be renaming folders to match the ID3 tags, the media players can take it from here…
Enjoy!
Gee, why didn’t I think of that -.-
whooosh!!!!!
( sound of this info flyin’ twenty-thousand feet above by head)