PHPBuilder - ID3v2 tag extraction



RSS Twitter
Snippets File Management

ID3v2 tag extraction

by: Anders Bruun Olsen
|
March 2, 2001

Version: 1.1

Type: Function

Category: File Management

License: GNU General Public License

Description: These functions can extract ID3v2 tags from MP3 files. Just pass the location of an MP3 file to extract_id3v2() and it will return a nice array containing all the frames from the tag. The code is not very pretty as I haven't got much time to spend on this. If anyone feels like cleaning up the code then please feel free to do so. I only ask that you send me a copy of the finished work ;) Enjoy



<?
/*
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..

The newest version of this file can be found at:

http://www.elffan.com/id3v2

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.

History:
02-03-2001 - Fixed a problem where the code would timeout on larger
             files. This means that at the moment only files with
			 the ID3v2 tag at the very beginning of the file can be
			 read.
01-03-2001 - First release.
*/

function rem_synchsafe ($tal)
{
	$tal = hexdec(bin2hex($tal));
	$tal1 = floor($tal/256) * 128 + ($tal%256);
	$tal2 = floor($tal1/32768) * 16384 + ($tal1%32768);
	$sluttal = floor($tal2/4194304) * 2097152 + ($tal2%4194304);
	return $sluttal;
};

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);
	$fp = goto_id3pos($filename,3);

	// 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;
};
?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Anders Bruun Olsen

Comment:



Comment:

(Maximum characters: 1200). You have characters left.