<?
/*
This file was written by Anders Bruun Olsen (anders@gerf.dk)
It is all released under the GNU GPL license and therefore free
for everybody to use.
I hope that someone will find it useful and perhaps clean it up
and make it a bit smoother..
For those in doubt, it is used by giving a filename (preferebly
with a full path) of an MP3 file containing an ID3v2 tag to
extract_id3v2().
An array is then returned containing all the frames in the tag.
*/
function get_flags ($fp)
{
// This function reads the flag bit and puts out something useful...
$flags = hexdec(bin2hex(fgetc($fp)));
if (($flags - 128) >= 0) {
// flag a is set (Unsynchronisation)
$flags = $flags - 128;
$flaglist .= "a,";
}
if (($flags - 64) >= 0) {
// flag b is set (Extended Header)
$flags = $flags - 64;
$flaglist .= "b,";
}
if (($flags - 32) >= 0) {
// flag c is set (Experimental indicator)
$flags = $flags - 32;
$flaglist .= "c,";
}
if (($flags - 16) >= 0) {
// flag d is set (Footer present)
$flags = $flags - 16;
$flaglist .= "d,";
}
if (($flags - 8) >= 0) {
// flag e is set (Unused in v2.4.0)
$flags = $flags - 8;
$flaglist .= "e,";
}
if (($flags - 4) >= 0) {
// flag f is set (Unused in v2.4.0)
$flags = $flags - 4;
$flaglist .= "f,";
}
if (($flags - 2) >= 0) {
// flag g is set (Unused in v2.4.0)
$flags = $flags - 2;
$flaglist .= "g,";
}
if (($flags - 1) >= 0) {
// flag h is set (Unused in v2.4.0)
$flags = $flags - 1;
$flaglist .= "h,";
}
if (strlen($flaglist) > 0)
{
$flaglist = substr($flaglist,0,strlen($flaglist)-1);
}
return $flaglist;
};
function get_id3pos ($filename)
{
// This function will return the position of the ID3v2 tag if found
$fp = fopen($filename,"r");
while(!feof($fp)){
$char = fgetc($fp);
if($char == "I") {
$char = fgetc($fp);
if ($char == "D") {
$char = fgetc($fp);
if ($char == "3") {
$id3pos = ftell($fp);
}
}
}
}
fclose($fp);
if (!$id3pos) {
return false;
} else {
return $id3pos;
}
};
function goto_id3pos ($filename,$id3pos)
{
// This function will open the file, go to the ID3v2 tag position and return the filepointer
$fp = fopen($filename,"r");
if (fseek($fp,$id3pos) != 0) {
die ("Could not go to ID3 tag\n");
}
return $fp;
};
function get_id3_version ($fp)
{
// This function gets the ID3v2 version from the file open at the filepointer and returns it
$ver1 = hexdec(bin2hex(fgetc($fp)));
$ver2 = hexdec(bin2hex(fgetc($fp)));
$version = "ID3v2." . $ver1 . "." . $ver2;
return $version;
};
function get_tagsize ($fp)
{
// This function gets the tagsize converts it from a synchsafe int to a normal int and returns it
for ($i = 0; $i < 4; $i++) {
$tagsize .= fgetc($fp);
}
$tagsize = rem_synchsafe($tagsize);
return $tagsize;
};
function extract_id3v2($filename)
{
// Find the position of the ID3v2 tag
if (!$id3pos = get_id3pos($filename)) {
die("No ID3v2 tag found in $filename\n");
};
// go to id3v2 tag in file
$fp = goto_id3pos($filename,$id3pos);
// Get the version
$id3ver = get_id3_version($fp);
$id3flags = get_flags($fp);
$tagsize = get_tagsize($fp);
$tagsize_left = $tagsize;
while ($tagsize_left > 1) {
unset($frameID);
unset($framesize);
unset($frame);
for ($i = 0; $i < 4; $i++) {
$frameID .= fgetc($fp);
}
for ($i = 0; $i < 4; $i++) {
$framesize .= fgetc($fp);
}
$framesize = rem_synchsafe($framesize);
if ($framesize >= 1)
{
$tagsize_left = $tagsize_left - ($framesize + 10);
$flag1 = get_flags($fp);
$flag2 = get_flags($fp);
$encoding = hexdec(bin2hex(fgetc($fp)));
if ($encoding <= 3) {
// This is the encoding bit
for ($i = 0; $i < ($framesize - 1); $i++) {
$frame .= fgetc($fp);
}
} else {
// This was not an encoding bit, it is part of the frame content
$frame = $encoding;
for ($i = 0; $i < $framesize; $i++) {
$frame .= fgetc($fp);
}
}
}
else
{
$frameID = "Padding";
$tagsize_left = 0;
}
if ($frameID == "SYLT" OR $frameID == "USLT" OR $frameID == "USER" OR $frameID == "COMM")
{
$frame = substr($frame,3,strlen($frame));
}
$framearray["$frameID"] = $frame;
};
fclose($fp);
return $framearray;
}
?>
Anders Olsen wrote :538
If you run into trouble with PHP timeouts it is because of the loop that locates the ID3v2 tag position. As most ID3v2 tags are found in the beginning of files I have modified the code to skip that loop and just read the tag from the start.
This means that files where the tag is not located in the beginning is not supported.
The newest version of this code can be found at:
http://www.elffan.com/id3v2