Getting ID3 Tags with PHP
ID3 (from the English “Identify an MP3”) is a metadata format most commonly used in MP3 files. An ID3 tag contains information about the track title, album, artist name, etc. This is the information Winamp and all other players use when playing a file to show which song is currently playing.
http://ru.wikipedia.org/wiki/ID3_(метаданные)
php_id3
http://ru2.php.net/manual/ru/book.id3.php
We ran into a lot of problems when using it. First, it couldn’t identify even a third of the test files, and second, it often crashes when opening a file. In the end, we abandoned it…
GetID3
http://getid3.sourceforge.net/
While looking for a better solution, I came across the GetID3 project and downloaded the latest version of this library (Latest 2.x beta version: 2.0.0-b4).
It supports the following tag formats:
- ID3v1 (v1.0 & v1.1)
- ID3v2 (v2.2, v2.3 & v2.4)
- APE tags (v1 & v2)
- (Ogg) VorbisComment
- Lyrics3 (v1 & v2)
You can find a more detailed description of its capabilities on the project page, but let’s move on and take a closer look at the library itself—in action, so to speak.
I added the library to the project, opened my favorite IDE, Eclipse, and it immediately notified me of an error on line 403 of write.id3v2.php. It turns out the developers forgot to open a block after else. A pretty glaring mistake, of course, but let’s chalk it up to the beta version :)
Moving on: everything works now, and processing is quite fast!
Usage example:
$getid3 = new getID3();
$getid3->encoding = 'UTF-8'; //указываем output-кодировку
$getid3->Analyze($filename); //путь до файла
var_dump($getid3->info['comments']); //посмотрим результаты работы
Dealing with the last character being cut off
Another problem: trimming the end removes one character too many. For example, “Алиса” becomes “Алис”. So, open module.tag.id3v2.php (338) and replace
$parsed_frame['data'] = substr($frame_data, 0, $frame_size);
with
$parsed_frame['data'] = substr($frame_data, 0, $frame_size +1);
**There’s also a problem with encoding detection
**
The problem is that this library cannot correctly detect the encoding, and that’s where all the trouble comes from… For example, our tags are in windows-1251, but the library identifies the encoding as iso-8859-1.
The trouble is that right at the start we set the output encoding (UTF-8), and it converts a windows-1251 string to utf-8, using iso-8859-1 and utf-8 as the respective parameters. So we have to convert from utf-8 to iso-8859-1, and then from windows-1251 to utf-8 again :)
Of course, you can tell it to leave everything in iso, but that won’t solve the problem… It still can’t detect which encoding the tags are in…
We solved the problem locally within our project, but I hope things will change once the library is out of beta. You can also use earlier versions of this library; perhaps they don’t have these problems.
Comments are closed. Read the original discussion in Russian →
