-
-
Save aircokol/cc417b1157fc389cd5bbd3dd373242fa to your computer and use it in GitHub Desktop.
Get MP3 bit rate and sample rate in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //returns an assoc array with bitRate (kbps) and sampleRate (khz) | |
| function getMP3BitRateSampleRate($filename) | |
| { | |
| if (!file_exists($filename)) { | |
| return false; | |
| } | |
| $bitRates = array( | |
| array(0,0,0,0,0), | |
| array(32,32,32,32,8), | |
| array(64,48,40,48,16), | |
| array(96,56,48,56,24), | |
| array(128,64,56,64,32), | |
| array(160,80,64,80,40), | |
| array(192,96,80,96,48), | |
| array(224,112,96,112,56), | |
| array(256,128,112,128,64), | |
| array(288,160,128,144,80), | |
| array(320,192,160,160,96), | |
| array(352,224,192,176,112), | |
| array(384,256,224,192,128), | |
| array(416,320,256,224,144), | |
| array(448,384,320,256,160), | |
| array(-1,-1,-1,-1,-1), | |
| ); | |
| $sampleRates = array( | |
| array(44100,48000,32000), | |
| array(22050,24000,16000), | |
| array(11025,12000,8000), | |
| ); | |
| $bToRead = 1024 * 6; | |
| $fileData = array('bitRate' => 0, 'sampleRate' => 0); | |
| $fp = fopen($filename, 'r'); | |
| if (!$fp) { | |
| return false; | |
| } | |
| //seek to 8kb before the end of the file | |
| fseek($fp, -1 * $bToRead, SEEK_END); | |
| $data = fread($fp, $bToRead); | |
| $bytes = unpack('C*', $data); | |
| $frames = array(); | |
| for ($o = 0; $o < count($bytes) - 4; $o++) { | |
| if (($bytes[$o] & 0xFF) == 0xFF && ($bytes[$o+1] & 0xE0) == 0xE0) { | |
| $frames[] = array_slice($bytes, $o-1, 4); | |
| } | |
| if (count($frames) <= 4) { //verify at least 4 frames to make sure its an mp3 | |
| $o += 3; | |
| continue; | |
| } | |
| //throw out the first result as it is usually wrong | |
| array_shift($frames); | |
| //loop through verified frames trying to get any information we can | |
| do { | |
| $header = array_shift($frames); | |
| $version = $header[1] & 0x18; | |
| $layer = $header[1] & 0x06; | |
| //get sample rate | |
| $srIndex = ($header[2] & 0x0C) >> 2; | |
| switch ($version) { | |
| case 0x18: | |
| $fileData['sampleRate'] = $sampleRates[0][$srIndex]; | |
| break; | |
| case 0x10: | |
| $fileData['sampleRate'] = $sampleRates[1][$srIndex]; | |
| break; | |
| case 0x00: | |
| $fileData['sampleRate'] = $sampleRates[2][$srIndex]; | |
| break; | |
| } | |
| //bit rate | |
| $brRow = ($header[2] & 0xF0) >> 4; | |
| if ($brRow != 15 && $brRow != 0) { | |
| if($version == 0x18 && $layer == 0x06) { | |
| $fileData['bitRate'] = $bitRates[$brRow][0]; | |
| } else if($version == 0x18 && $layer == 0x04) { | |
| $fileData['bitRate'] = $bitRates[$brRow][1]; | |
| } else if($version == 0x18 && $layer == 0x02) { | |
| $fileData['bitRate'] = $bitRates[$brRow][2]; | |
| } else if(($version == 0x10 || $version == 0x00) && $layer == 0x06) { | |
| $fileData['bitRate'] = $bitRates[$brRow][3]; | |
| } else if(($version == 0x10 || $version == 0x00) && ($layer == 0x04 || $layer == 0x02) ) { | |
| $fileData['bitRate'] = $bitRates[$brRow][4]; | |
| } | |
| } | |
| } while (count($frames) && (!$fileData['bitRate'] || !$fileData['sampleRate'])); | |
| break; | |
| } | |
| //if both are 0, then it probably isn't an MP3 | |
| return $fileData; | |
| } | |
| /* | |
| Example: | |
| if (!empty($_FILES['mp3'])) { | |
| $data = verifyMP3File($_FILES['mp3']['tmp_name']); | |
| } | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment