Skip to content

Instantly share code, notes, and snippets.

@aircokol
Forked from jameshartig/gist:2357002
Created October 2, 2016 17:08
Show Gist options
  • Select an option

  • Save aircokol/cc417b1157fc389cd5bbd3dd373242fa to your computer and use it in GitHub Desktop.

Select an option

Save aircokol/cc417b1157fc389cd5bbd3dd373242fa to your computer and use it in GitHub Desktop.
Get MP3 bit rate and sample rate in PHP
//returns an assoc array with bitRate (kbps) and sampleRate (hz)
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(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;
$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++) {
//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;
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);
$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;
}
/*
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