MP3::ID3Lib

acme on 2003-01-29T11:21:26

Blech has been working on categorising all his MP3s, and needed to be able to read and write ID3v2 tags. Now, MP3::Info is pretty cool, but doesn't support writing of tags. Blech pointed me at the ID3Lib library, "an open-source, cross-platform software development library for reading, writing, and manipulating ID3v1 and ID3v2 tags", which seems to be what many MP3 tools use. And I foolishly agreed to try and code up a Perl module for him.

What is all this ID3 nonsense, you ask? Well it's be easy to explain if the ID3.org website were up more often. ID3 tags are small pieces of information stored inside the MP3 file. They can contain bits of metadata about the MP3, such as album name, song name, artist, original artist, genre, composer, year of release, additional comment fields, and many more. Unfortunately, there have been many versions of the ID3 spec and it has slowly been getting more and more complicated. Oh, and the tags are typed. And may have other fields than simply "value". And tags have been renamed in newer versions of the spec for no real reason other than to confuse me. A simple key -> value frame at the end of the MP3 file would have been so much simpler, but unfortunately we can't change the world overnight.

Anyway, ID3Lib reads and writes these tags. The actual library is in C++ but there is a cut-down C interface which I picked as I know no C. After refreshing my XS and typemap knowledge, it all pretty much fell into place. One strange thing was having to use g++ to compile the XS. Is there a more-portable way to find a C++ compiler than:

# In Makefile.PL
'CC' => 'g++',
'LD' => 'g++',
The current docs and API are a little lacking and there's a small memory leak, but it works! It's up on the CPAN, phew.

And thanks to Pudge for letting me use his test MP3s...


Am I Going Mad?

davorg on 2003-01-29T11:50:19

MP3::Info is pretty cool, but doesn't support writing of tags.

I could have sworn that I've been using MP3::Info to write tags for months. The docs mention a set_mp3tag function.

Re:Am I Going Mad?

acme on 2003-01-29T12:04:55

Oooops, I should have said "but doesn't support writing of ID3v2 tags".

Re:Am I Going Mad?

blech on 2003-01-29T12:22:58

As acme notes, MP3::Info doesn't currently support writing id3v2 tags. However, he doesn't explain why this is so important.

Firstly, id3v1 has length limits on all the fields it supports, usually about 40 or so characters. Secondly, id3v1 only supports six (I think) basic tags.

As I like to keep the file names of my mp3s short (so I can still see them with the Mac OS 9 Finder, amongst other reasons), that means the id3 tags have to keep all the metadata about the mp3s, so id3v1 just doesn't cut it. (Not entirely coincidentally, I also use iTunes, which writes id3v2.2 tags, so I'd rather not lose the data.)

I have a lot more wittering to do on the subject of mp3s and id3 tags; I'll get around to it soon. Promise.

Re:Am I Going Mad?

pudge on 2003-01-29T13:18:07

It is 30 characters per field for ID3v1, for artist, album, title, and comment. If you use ID3v1.1 (which has a track number field), comment is then limited to 28. Year is 4.

I have over a dozen MP3::Info patches to apply; when Apple sends me my new PowerBook, I am going to work more on them. One of them has to do with writing ID3v2 tags. I've not tried it yet, but someone wrote MP3::Info::set_mp3v2tag, and has given me permission to integrate it into MP3::Info. Have you tried this module? I've not yet had the chance. I hope it fits the bill, or is at least a good start.

Re: set_mp3v2tag

blech on 2003-01-29T13:34:35

I have tried set_mp3v2tag, yes. It works fairly well for the core fields, and is a pretty good starting point.

My one complaint is that it doesn't set comments correctly, because id3v2 allows multiple comment frames, distinguished by the language and description. I have a (three-line) local patch that uses 'eng' as the default language, which is probably acceptable as a convenience.

Re: set_mp3v2tag

pudge on 2003-01-29T13:49:06

Well, I am open to patches and ideas on how to deal with this stuff in MP3::Info, if anyone wants to help out.

id3lib rocks!

gnat on 2003-01-29T17:43:09

Yes, id3lib is fantastic. It was the only thing I could find to convert the tags on my old RealJukebox-ripped mp3s into a format that iTunes could deal with. iTunes' conversion of tags doesn't do anything, as far as I could tell.

Why do you need a Perl interface? I've been doing a ton of things like this: % ls -1 "His Dark"* | perl -nle '/(.*) - (\d+) - (.*)/; system("id3tag", "-aBBC", "-A$1", "-s$3", "-t$2", "-T3", $_)' (for those without id3lib, that takes His Dark Materials - 1 - The Amber Spyglass.mp3 and tags it with the artist as BBC, the album as "His Dark Materials", track number 1/3, and song title "The Amber Spyglass")

And when I resample mp3s (e.g., radio shows from 128 to 64 without loss of quality), id3cp lets me give the new files the same attributes as the old. Sweet.

It's been a long time since a single piece of code made my life so much easier.

--Nat

Re:id3lib rocks!

gnat on 2003-01-29T17:47:05

Lesson learned :-) Always preview.

% ls -1 "His Dark"* | perl -nle '/(.*) - (\d+) - (.*)/; system("id3tag", "-aBBC", "-A$1", "-s$3", "-t$2", "-T3", $_)'
--Nat

Re:id3lib rocks!

acme on 2003-01-30T09:54:51

Tempting indeed. This XS stuff just makes my head hurt. Maybe command lines are the future ;-)

Re:id3lib rocks!

pudge on 2003-02-02T04:53:04

Why wouldn't MP3::Info work for you? And where's the bug report??