PX : code

Mp3 Info Function by CheSter
Download this code


<?php

//
// This code made possible by Jim Beam black label bourbon.
// and the info found on http://www.dv.co.yu/mpgscript/mpeghdr.htm .
//
// Coded by Chester@hackster.com
//
// Returns a single demensional array. 
//
// filename,filesize,length in seconds,bitrate,sample rate,channel mode,mpeg version,layer,crc,copyright,original
//
// Enjoy... Oh BTW this code REALLY sucks
//

function padd (&$inf)

// Dodgy ass function to pad binary strings so they represent the correct 8 bits.
    
    
{
        while (
strlen($inf)<8)
            {
                
$inf "0".$inf;
            }
    }

function 
js_alert($msg

// Function to spit out little window when an error occur's

{
echo (
"\n<script language='javascript'>\n" .
"    <!-- \n" .
"    alert (\"$msg\");\n" .
"    // --> \n" .
"</script>\n"
);
}
    
function 
mp3info($filename)

{

// Ensure file exists!
    
if (!$file fopen($filename,"rb"))
    {
        
js_alert('Yo I cant find\\n$filename');
        exit;
    }

// Checking to make sure I can find Frame Sync

while (!feof($file))
    {
        
$tmp=fgetc($file);
        if (
ord($tmp)==255)
            {
                
$tmp=fgetc($file);
                if (
substr((decbin(ord($tmp))),0,3)=="111")
                    {
                        break;
                    }
            }
    }

// If end of file is reached before Frame Sync is found then bail...

if (feof($file))
    {
        
js_alert('Ummmm are you sure this is a MP3 ??');
        
fclose($file);
        return(
1);
    }

// We have declared all engines go.

// Assign filesize

$filesize=filesize($filename);

// Assign all important information to $bitstream variable.

$inf=decbin(ord($tmp));
padd($inf);
$bitstream $inf;
$tmp=fgetc($file);
$inf=decbin(ord($tmp));
padd($inf);
$bitstream $bitstream.$inf;
$tmp=fgetc($file);
$inf=decbin(ord($tmp));
padd($inf);
$bitstream $bitstream.$inf;

// $bitstream now totals the 3 important bytes of the header of this frame.

// Determine Version of Mpeg.

switch (substr($bitstream,3,2))
    {
        case 
"00":
            
$version="2.5";
            break;
        case 
"01":
            
$version="0";
            break;
        case 
"10":
            
$version="2";
            break;
        case 
"11":
            
$version="1";
            break;
    }

// Determine Layer.

switch (substr($bitstream,5,2))
    {
        case 
"00":
            
$layer="0";
            break;
        case 
"01":
            
$layer="3";
            break;
        case 
"10":
            
$layer="2";
            break;
        case 
"11":
            
$layer="1";
            break;
    }

// Determine CRC checking enabled / disabled 1==disabled

$crc substr($bitstream,7,1);

// Determine Bitrate

// Setting an index variable ... trust me in this state tis the only way I can think of doing it...

if (($version=="1")&($layer=="1"))
    {
        
$index="1";
    }
elseif ((
$version=="1")&($layer=="2"))
    {
        
$index="2";
    }
elseif (
$version=="1")
    {
        
$index="3";
    }
elseif (
$layer=="1")
    {
        
$index="4";
    }
else    
    {
        
$index="5";
    }

switch (
substr($bitstream,8,4))
    {
        case 
"0000":
            
$bitrate="free";
            break;
        case 
"0001":
            if ((
$layer>1)and($version>1))
                {
                    
$bitrate="8000";
                }
            else
                {
                    
$bitrate="32000";
                }
            break;
        case 
"0010":
            switch (
$index)
                {
                    case 
"1":
                        
$bitrate="64000";
                        break;
                    case 
"2":
                        
$bitrate="48000";
                        break;
                    case 
"3":
                        
$bitrate="40000";
                        break;
                    case 
"4":
                        
$bitrate="48000";
                        break;
                    case 
"5":
                        
$bitrate="16000";
                        break;
                }
            break;
        case 
"0011":
            switch (
$index)
                {
                    case 
"1":
                        
$bitrate="96000";
                        break;
                    case 
"2":
                        
$bitrate="56000";
                        break;
                    case 
"3":
                        
$bitrate="48000";
                        break;
                    case 
"4":
                        
$bitrate="56000";
                        break;
                    case 
"5":
                        
$bitrate="24000";
                        break;
                }
            break;
        case 
"0100":
            switch (
$index)
                {
                    case 
"1":
                        
$bitrate="128000";
                        break;
                    case 
"2":
                        
$bitrate="64000";
                        break;
                    case 
"3":
                        
$bitrate="56000";
                        break;
                    case 
"4":
                        
$bitrate="64000";
                        break;
                    case 
"5":
                        
$bitrate="32000";
                        break;
                }
            break;
        case 
"0101":
            switch (
$index)
                {
                    case 
"1":
                        
$bitrate="160000";
                        break;
                    case 
"2":
                        
$bitrate="80000";
                        break;
                    case 
"3":
                        
$bitrate="64000";
                        break;
                    case 
"4":
                        
$bitrate="80000";
                        break;
                    case 
"5":
                        
$bitrate="40000";
                        break;
                }
            break;
        case 
"0110":
            switch (
$index)
                {
                    case 
"1":
                        
$bitrate="192000";
                        break;
                    case 
"2":
                        
$bitrate="96000";
                        break;
                    case 
"3":
                        
$bitrate="80000";
                        break;
                    case 
"4":
                        
$bitrate="96000";
                        break;
                    case 
"5":
                        
$bitrate="48000";
                        break;
                }
            break;
        case 
"0111":
            switch (
$index)
                {
                    case 
"1":
                        
$bitrate="224000";
                        break;
                    case 
"2":
                        
$bitrate="112000";
                        break;
                    case 
"3":
                        
$bitrate="96000";
                        break;
                    case 
"4":
                        
$bitrate="112000";
                        break;
                    case 
"5":
                        
$bitrate="56000";
                        break;
                }
            break;
        case 
"1000":
            switch (
$index)
                {
                    case 
"1":
                        
$bitrate="256000";
                        break;
                    case 
"2":
                        
$bitrate="128000";
                        break;
                    case 
"3":
                        
$bitrate="112000";
                        break;
                    case 
"4":
                        
$bitrate="128000";
                        break;
                    case 
"5":
                        
$bitrate="64000";
                        break;
                }
            break;
        case 
"1001":
            switch (
$index)
                {
                    case 
"1":
                        
$bitrate="288000";
                        break;
                    case 
"2":
                        
$bitrate="160000";
                        break;
                    case 
"3":
                        
$bitrate="128000";
                        break;
                    case 
"4":
                        
$bitrate="144000";
                        break;
                    case 
"5":
                        
$bitrate="80000";
                        break;
                }
            break;
        case 
"1010":
            switch (
$index)
                {
                    case 
"1":
                        
$bitrate="320000";
                        break;
                    case 
"2":
                        
$bitrate="192000";
                        break;
                    case 
"3":
                        
$bitrate="160000";
                        break;
                    case 
"4":
                        
$bitrate="160000";
                        break;
                    case 
"5":
                        
$bitrate="96000";
                        break;
                }
            break;
        case 
"1011":
            switch (
$index)
                {
                    case 
"1":
                        
$bitrate="352000";
                        break;
                    case 
"2":
                        
$bitrate="224000";
                        break;
                    case 
"3":
                        
$bitrate="192000";
                        break;
                    case 
"4":
                        
$bitrate="176000";
                        break;
                    case 
"5":
                        
$bitrate="112000";
                        break;
                }
            break;
        case 
"1100":
            switch (
$index)
                {
                    case 
"1":
                        
$bitrate="384000";
                        break;
                    case 
"2":
                        
$bitrate="256000";
                        break;
                    case 
"3":
                        
$bitrate="224000";
                        break;
                    case 
"4":
                        
$bitrate="192000";
                        break;
                    case 
"5":
                        
$bitrate="128000";
                        break;
                }
            break;
        case 
"1101":
            switch (
$index)
                {
                    case 
"1":
                        
$bitrate="416000";
                        break;
                    case 
"2":
                        
$bitrate="320000";
                        break;
                    case 
"3":
                        
$bitrate="256000";
                        break;
                    case 
"4":
                        
$bitrate="224000";
                        break;
                    case 
"5":
                        
$bitrate="144000";
                        break;
                }
            break;
        case 
"1110":
            switch (
$index)
                {
                    case 
"1":
                        
$bitrate="448000";
                        break;
                    case 
"2":
                        
$bitrate="384000";
                        break;
                    case 
"3":
                        
$bitrate="320000";
                        break;
                    case 
"4":
                        
$bitrate="256000";
                        break;
                    case 
"5":
                        
$bitrate="160000";
                        break;
                }
            break;
        case 
"1111":
            
$bitrate="bad";
            break;
    }

// Determine Sample Rate

switch ($version)
    {
        case 
"1":
            switch (
substr($bitstream,12,2))
                {
                    case 
"00":
                        
$sample="44100";
                        break;
                    case 
"01":
                        
$sample="48000";
                        break;
                    case 
"10":
                        
$sample="32000";
                        break;
                    case 
"11":
                        
$sample="reserved";
                        break;
                }
            break;
        case 
"2":
            switch (
substr($bitstream,12,2))
                {
                    case 
"00":
                        
$sample="22050";
                        break;
                    case 
"01":
                        
$sample="24000";
                        break;
                    case 
"10":
                        
$sample="16000";
                        break;
                    case 
"11":
                        
$sample="reserved";
                        break;
                }
            break;
        case 
"2.5":
            switch (
substr($bitstream,12,2))
                {
                    case 
"00":
                        
$sample="11025";
                        break;
                    case 
"01":
                        
$sample="12000";
                        break;
                    case 
"10":
                        
$sample="8000";
                        break;
                    case 
"11":
                        
$sample="reserved";
                        break;
                }
            break;
    }

// Determine whether padding is set on. 0 == no & 1 == yes

$padding substr($bitstream,14,1);

// Determine the private bit's value. Dont know what for though?

$private substr($bitstream,15,1);

// Determine Channel mode

switch (substr($bitstream,16,2))
    {
        case 
"00":
            
$cmode="Stereo";
            break;
        case 
"01":
            
$cmode="Joint Stereo";
            break;
        case 
"10":
            
$cmode="Dual Channel";
            break;
        case 
"11":
            
$cmode="Mono";
            break;
    }
    
/*

// Determine Mode Extension, actually who cares for the moment

switch (substr($bitstream,18,2))
    {
        case "00":
            $mext="0";
            break;
        case "01":
            $mext="3";
            break;
        case "10":
            $mext="2";
            break;
        case "11":
            $mext="1";
            break;
    }

*/
    
// Determine Copyright 0 == no & 1 == yes

$copyright substr($bitstream,20,1);

// Determine Original 0 == Copy & 1 == Original

$original substr($bitstream,21,1);

// Determine Emphasis

switch (substr($bitstream,22,2))
    {
        case 
"00":
            
$emphasis="none";
            break;
        case 
"01":
            
$emphasis="50/15 ms";
            break;
        case 
"10":
            
$emphasis="reserved";
            break;
        case 
"11":
            
$emphasis="CCIT J.17";
            break;
    }

// Determine number of frames.

if ((isset($sample)) and (isset($bitrate)))
{
if (
$layer=="1")
    {
        
$frames=floor($filesize/(floor(((12*$bitrate)/($sample+$padding))*4)));    
    }
else
    {
        
$frames=floor($filesize/(floor((144*$bitrate)/($sample))));
    }
    
// Determine number of seconds in song.

if ($layer=="1")
    {
        
$seconds=floor((384/$sample)*$frames);
    }
else
    {
        
$seconds=floor((1152/$sample)*$frames);
    }
}
fclose($file);

$fred[]=$filename;
$fred[]=$filesize;
$fred[]=$seconds;
$fred[]=$bitrate;
$fred[]=$sample;
$fred[]=$cmode;
$fred[]=$version;
$fred[]=$layer;
$fred[]=$crc;
$fred[]=$copyright;
$fred[]=$original;

return(
$fred);

}

?>

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.