-
-
Save aircokol/cc417b1157fc389cd5bbd3dd373242fa to your computer and use it in GitHub Desktop.
Revisions
-
jameshartig revised this gist
Apr 15, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -97,6 +97,6 @@ function getMP3BitRateSampleRate($filename) /* Example: if (!empty($_FILES['mp3'])) { $data = getMP3BitRateSampleRate($_FILES['mp3']['tmp_name']); } */ -
jameshartig revised this gist
Apr 15, 2012 . 1 changed file with 36 additions and 23 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -29,7 +29,7 @@ function getMP3BitRateSampleRate($filename) array(22050,24000,16000), //mpeg 2 array(44100,48000,32000), //mpeg 1 ); $bToRead = 1024 * 12; $fileData = array('bitRate' => 0, 'sampleRate' => 0); $fp = fopen($filename, 'r'); @@ -42,43 +42,56 @@ function getMP3BitRateSampleRate($filename) $bytes = unpack('C*', $data); $frames = array(); $lastFrameVerify = null; for ($o = 1; $o < count($bytes) - 4; $o++) { //http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html //header is AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM if (($bytes[$o] & 255) == 255 && ($bytes[$o+1] & 224) == 224) { $frame = array(); $frame['version'] = ($bytes[$o+1] & 24) >> 3; //get BB (0 -> 3) $frame['layer'] = abs((($bytes[$o+1] & 6) >> 1) - 4); //get CC (1 -> 3), then invert $srIndex = ($bytes[$o+2] & 12) >> 2; //get FF (0 -> 3) $brRow = ($bytes[$o+2] & 240) >> 4; //get EEEE (0 -> 15) $frame['padding'] = ($bytes[$o+2] & 2) >> 1; //get G if ($frame['version'] != 1 && $frame['layer'] > 0 && $srIndex < 3 && $brRow != 15 && $brRow != 0 && (!$lastFrameVerify || $lastFrameVerify === $bytes[$o+1])) { //valid frame header //calculate how much to skip to get to the next header $frame['sampleRate'] = $sampleRates[$frame['version']][$srIndex]; if ($frame['version'] & 1 == 1) { $frame['bitRate'] = $bitRates[$brRow][$frame['layer']-1]; //v1 and l1,l2,l3 } else { $frame['bitRate'] = $bitRates[$brRow][($frame['layer'] & 2 >> 1)+3]; //v2 and l1 or l2/l3 (3 is the offset in the arrays) } if ($frame['layer'] == 1) { $frame['frameLength'] = (12 * $frame['bitRate'] * 1000 / $frame['sampleRate'] + $frame['padding']) * 4; } else { $frame['frameLength'] = 144 * $frame['bitRate'] * 1000 / $frame['sampleRate'] + $frame['padding']; } $frames[] = $frame; $lastFrameVerify = $bytes[$o+1]; $o += floor($frame['frameLength'] - 1); } else { $frames = array(); $lastFrameVerify = null; } } if (count($frames) < 3) { //verify at least 3 frames to make sure its an mp3 continue; } $header = array_pop($frames); $fileData['sampleRate'] = $header['sampleRate']; $fileData['bitRate'] = $header['bitRate']; break; } return $fileData; } /* -
jameshartig revised this gist
Apr 14, 2012 . 1 changed file with 20 additions and 36 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -24,9 +24,10 @@ function getMP3BitRateSampleRate($filename) array(-1,-1,-1,-1,-1), ); $sampleRates = array( array(11025,12000,8000), //mpeg 2.5 array(0,0,0), array(22050,24000,16000), //mpeg 2 array(44100,48000,32000), //mpeg 1 ); $bToRead = 1024 * 6; @@ -44,8 +45,17 @@ function getMP3BitRateSampleRate($filename) for ($o = 0; $o < count($bytes) - 4; $o++) { //header is AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM if (($bytes[$o] & 0xFF) == 0xFF && ($bytes[$o+1] & 0xE0) == 0xE0) { $frame = array(); $frame['version'] = ($bytes[$o+1] & 24) >> 3; //get BB (0 -> 3) $frame['layer'] = abs((($bytes[$o+1] & 6) >> 1) - 4); //get CC (1 -> 3), then invert $frame['srI'] = ($bytes[$o+2] & 12) >> 2; //get FF (0 -> 3) $frame['brR'] = ($bytes[$o+2] & 240) >> 4; //get EEEE (0 -> 15) if ($frame['version'] != 1 && $frame['layer'] > 0 && $frame['srI'] < 3 && $frame['brR'] != 15 && $frame['brR'] != 0) { //valid frame! $frames[] = array_slice($bytes, $o-1, 4); } } if (count($frames) <= 4) { //verify at least 4 frames to make sure its an mp3 $o += 3; @@ -57,44 +67,18 @@ function getMP3BitRateSampleRate($filename) //loop through verified frames trying to get any information we can do { $header = array_shift($frames); $fileData['sampleRate'] = $sampleRates[$header['version']][$header['srI']]; if ($header['version'] & 1 == 1) { $fileData['bitRate'] = $bitRates[$header['brR']][$header['layer']-1]; //v1 and l1,l2,l3 } else { $fileData['bitRate'] = $bitRates[$header['brR']][($header['layer'] & 2 >> 1)+3]; //v2 and l1 or l2/l3 (3 is the offset in the arrays) } } while (count($frames) && (!$fileData['bitRate'] || !$fileData['sampleRate'])); break; } //if both are 0 then its not an mp3 return $fileData; } /* -
jameshartig revised this gist
Apr 11, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ //returns an assoc array with bitRate (kbps) and sampleRate (hz) function getMP3BitRateSampleRate($filename) { if (!file_exists($filename)) { -
jameshartig created this gist
Apr 11, 2012 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,105 @@ //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']); } */