您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php获取mp3音频信息实例教程

2025/10/6 14:57:17发布10次查看
php获取mp3音频信息
很早之前在网上看到一个获取 mp3 音频信息的 php 类。如:播放时长、文件大小、文件编码等等
<?phpclass mp3file{ protected $block; protected $blockpos; protected $blockmax; protected $blocksize; protected $fd; protected $bitpos; protected $mp3data; public function __construct($filename) { $this->powarr = array(0=>1,1=>2,2=>4,3=>8,4=>16,5=>32,6=>64,7=>128); $this->blockmax= 1024; $this->mp3data = array(); $this->mp3data['filesize'] = filesize($filename); $this->fd = fopen($filename,'rb'); $this->prefetchblock(); $this->readmp3frame(); } public function __destruct() { fclose($this->fd); } //------------------- public function get_metadata() { return $this->mp3data; } protected function readmp3frame() { $iscbrmp3=true; if ($this->startswithid3()) $this->skipid3tag(); else if ($this->containsvbrxing()) { $this->mp3data['encoding'] = 'vbr'; $iscbrmp3=false; } else if ($this->startswithpk()) { $this->mp3data['encoding'] = 'unknown'; $iscbrmp3=false; } if ($iscbrmp3) { $i = 0; $max=5000; //look in 5000 bytes... //the largest framesize is 4609bytes(256kbps@8000hz mp3) for($i=0; $i<$max; $i++) { //looking for 1111 1111 111 (frame synchronization bits) if ($this->getnextbyte()==0xff) if ($this->getnextbit() && $this->getnextbit() && $this->getnextbit()) break; } if ($i==$max) $iscbrmp3=false; } if ($iscbrmp3) { $this->mp3data['encoding' ] = 'cbr'; $this->mp3data['mpeg version' ] = $this->getnextbits(2); $this->mp3data['layer description'] = $this->getnextbits(2); $this->mp3data['protection bit' ] = $this->getnextbits(1); $this->mp3data['bitrate index' ] = $this->getnextbits(4); $this->mp3data['sampling freq idx'] = $this->getnextbits(2); $this->mp3data['padding bit' ] = $this->getnextbits(1); $this->mp3data['private bit' ] = $this->getnextbits(1); $this->mp3data['channel mode' ] = $this->getnextbits(2); $this->mp3data['mode extension' ] = $this->getnextbits(2); $this->mp3data['copyright' ] = $this->getnextbits(1); $this->mp3data['original media' ] = $this->getnextbits(1); $this->mp3data['emphasis' ] = $this->getnextbits(1); $this->mp3data['bitrate' ] = mp3file::bitratelookup($this->mp3data); $this->mp3data['sampling rate' ] = mp3file::samplelookup($this->mp3data); $this->mp3data['frame size' ] = mp3file::getframesize($this->mp3data); $this->mp3data['length' ] = mp3file::getduration($this->mp3data,$this->tell2()); $this->mp3data['length mm:ss' ] = mp3file::seconds_to_mmss($this->mp3data['length']); if ($this->mp3data['bitrate' ]=='bad' || $this->mp3data['bitrate' ]=='free' || $this->mp3data['sampling rate']=='unknown' || $this->mp3data['frame size' ]=='unknown' || $this->mp3data['length' ]=='unknown') $this->mp3data = array('filesize'=>$this->mp3data['filesize'], 'encoding'=>'unknown'); } else { if(!isset($this->mp3data['encoding'])) $this->mp3data['encoding'] = 'unknown'; } } protected function tell() { return ftell($this->fd); } protected function tell2() { return ftell($this->fd)-$this->blockmax +$this->blockpos-1; } protected function startswithid3() { return ($this->block[1]==73 && //i $this->block[2]==68 && //d $this->block[3]==51); //3 } protected function startswithpk() { return ($this->block[1]==80 && //p $this->block[2]==75); //k } protected function containsvbrxing() { //echo "<!--".$this->block[37]." ".$this->block[38]."-->"; //echo "<!--".$this->block[39]." ".$this->block[40]."-->"; return( ($this->block[37]==88 && //x 0x58 $this->block[38]==105 && //i 0x69 $this->block[39]==110 && //n 0x6e $this->block[40]==103) //g 0x67/* || ($this->block[21]==88 && //x 0x58 $this->block[22]==105 && //i 0x69 $this->block[23]==110 && //n 0x6e $this->block[24]==103) //g 0x67*/ ); } protected function debugbytes() { for($j=0; $j<10; $j++) { for($i=0; $i<8; $i++) { if ($i==4) echo " "; echo $this->getnextbit(); } echo "<br>"; } } protected function prefetchblock() { $block = fread($this->fd, $this->blockmax); $this->blocksize = strlen($block); $this->block = unpack("c*", $block); $this->blockpos=0; } protected function skipid3tag() { $bits=$this->getnextbits(24);//id3 $bits.=$this->getnextbits(24);//v.v flags //3 bytes 1 version byte 2 byte flags $arr = array(); $arr['id3v2 major version'] = bindec(substr($bits,24,8)); $arr['id3v2 minor version'] = bindec(substr($bits,32,8)); $arr['id3v2 flags' ] = bindec(substr($bits,40,8)); if (substr($bits,40,1)) $arr['unsynchronisation']=true; if (substr($bits,41,1)) $arr['extended header']=true; if (substr($bits,42,1)) $arr['experimental indicator']=true; if (substr($bits,43,1)) $arr['footer present']=true; $size = ""; for($i=0; $i<4; $i++) { $this->getnextbit();//skip this bit, should be 0 $size.= $this->getnextbits(7); } $arr['id3v2 tags size']=bindec($size);//now the size is in bytes; if ($arr['id3v2 tags size'] - $this->blockmax>0) { fseek($this->fd, $arr['id3v2 tags size']+10 ); $this->prefetchblock(); if (isset($arr['footer present']) && $arr['footer present']) { for($i=0; $i<10; $i++) $this->getnextbyte();//10 footer bytes } } else { for($i=0; $i<$arr['id3v2 tags size']; $i++) $this->getnextbyte(); } } protected function getnextbit() { if ($this->bitpos==8) return false; $b=0; $whichbit = 7-$this->bitpos; $mult = $this->powarr[$whichbit]; //$mult = pow(2,7-$this->pos); $b = $this->block[$this->blockpos+1] & $mult; $b = $b >> $whichbit; $this->bitpos++; if ($this->bitpos==8) { $this->blockpos++; if ($this->blockpos==$this->blockmax) //end of block reached { $this->prefetchblock(); } else if ($this->blockpos==$this->blocksize) {//end of short block reached (shorter than blockmax) return;//eof } $this->bitpos=0; } return $b; } protected function getnextbits($n=1) { $b=""; for($i=0; $i<$n; $i++) $b.=$this->getnextbit(); return $b; } protected function getnextbyte() { if ($this->blockpos>=$this->blocksize) return; $this->bitpos=0; $b=$this->block[$this->blockpos+1]; $this->blockpos++; return $b; } //----------------------------------------------------------------------------- public static function is_layer1(&$mp3) { return ($mp3['layer description']=='11'); } public static function is_layer2(&$mp3) { return ($mp3['layer description']=='10'); } public static function is_layer3(&$mp3) { return ($mp3['layer description']=='01'); } public static function is_mpeg10(&$mp3) { return ($mp3['mpeg version']=='11'); } public static function is_mpeg20(&$mp3) { return ($mp3['mpeg version']=='10'); } public static function is_mpeg25(&$mp3) { return ($mp3['mpeg version']=='00'); } public static function is_mpeg20or25(&$mp3) { return ($mp3['mpeg version']{1}=='0'); } //----------------------------------------------------------------------------- public static function bitratelookup(&$mp3) { //bits v1,l1 v1,l2 v1,l3 v2,l1 v2,l2&l3 $array = array(); $array['0000']=array('free','free','free','free','free'); $array['0001']=array( '32', '32', '32', '32', '8'); $array['0010']=array( '64', '48', '40', '48', '16'); $array['0011']=array( '96', '56', '48', '56', '24'); $array['0100']=array( '128', '64', '56', '64', '32'); $array['0101']=array( '160', '80', '64', '80', '40'); $array['0110']=array( '192', '96', '80', '96', '48'); $array['0111']=array( '224', '112', '96', '112', '56'); $array['1000']=array( '256', '128', '112', '128', '64'); $array['1001']=array( '288', '160', '128', '144', '80'); $array['1010']=array( '320', '192', '160', '160', '96'); $array['1011']=array( '352', '224', '192', '176', '112'); $array['1100']=array( '384', '256', '224', '192', '128'); $array['1101']=array( '416', '320', '256', '224', '144'); $array['1110']=array( '448', '384', '320', '256', '160'); $array['1111']=array( 'bad', 'bad', 'bad', 'bad', 'bad'); $whichcolumn=-1; if (mp3file::is_mpeg10($mp3) && mp3file::is_layer1($mp3) )//v1,l1 $whichcolumn=0; else if (mp3file::is_mpeg10($mp3) && mp3file::is_layer2($mp3) )//v1,l2 $whichcolumn=1; else if (mp3file::is_mpeg10($mp3) && mp3file::is_layer3($mp3) )//v1,l3 $whichcolumn=2; else if (mp3file::is_mpeg20or25($mp3) && mp3file::is_layer1($mp3) )//v2,l1 $whichcolumn=3; else if (mp3file::is_mpeg20or25($mp3) && (mp3file::is_layer2($mp3) || mp3file::is_layer3($mp3)) ) $whichcolumn=4;//v2, l2||l3 if (isset($array[$mp3['bitrate index']][$whichcolumn])) return $array[$mp3['bitrate index']][$whichcolumn]; else return "bad"; } //----------------------------------------------------------------------------- public static function samplelookup(&$mp3) { //bits mpeg1 mpeg2 mpeg2.5 $array = array(); $array['00'] =array('44100','22050','11025'); $array['01'] =array('48000','24000','12000'); $array['10'] =array('32000','16000','8000'); $array['11'] =array('res','res','res'); $whichcolumn=-1; if (mp3file::is_mpeg10($mp3)) $whichcolumn=0; else if (mp3file::is_mpeg20($mp3)) $whichcolumn=1; else if (mp3file::is_mpeg25($mp3)) $whichcolumn=2; if (isset($array[$mp3['sampling freq idx']][$whichcolumn])) return $array[$mp3['sampling freq idx']][$whichcolumn]; else return 'unknown'; } //----------------------------------------------------------------------------- public static function getframesize(&$mp3) { if ($mp3['sampling rate']>0) { return ceil((144 * $mp3['bitrate']*1000)/$mp3['sampling rate']) + $mp3['padding bit']; } return 'unknown'; } //----------------------------------------------------------------------------- public static function getduration(&$mp3,$startat) { if ($mp3['bitrate']>0) { $kbps = ($mp3['bitrate']*1000)/8; $datasize = ($mp3['filesize'] - ($startat/8)); $length = $datasize / $kbps; return sprintf("%d", $length); } return "unknown"; } //----------------------------------------------------------------------------- public static function seconds_to_mmss($duration) { return sprintf("%d:%02d", ($duration /60), $duration %60 ); }}
调用
<?phpinclude_once 'mp3file.class.php';function mp3time($file) { $m = new mp3file($file); $a = $m->get_metadata(); return $a['length mm:ss'] ? $a['length mm:ss'] : 0;}function mp3info($file) { $m = new mp3file($file); return $m->get_metadata();}$_time = mp3time('beyond-情人.mp3');echo "歌曲时间长:{$_time}\n";$_info = mp3info('beyond-情人.mp3');print_r($_info);
输出信息
→ php mp3.php歌曲时间长:5:15array( [filesize] => 7576152 [encoding] => cbr [mpeg version] => 11 [layer description] => 01 [protection bit] => 1 [bitrate index] => 1011 [sampling freq idx] => 00 [padding bit] => 0 [private bit] => 0 [channel mode] => 00 [mode extension] => 00 [copyright] => 0 [original media] => 1 [emphasis] => 0 [bitrate] => 192 [sampling rate] => 44100 [frame size] => 627 [length] => 315 [length mm:ss] => 5:15)
推荐教程:《php视频教程》
以上就是php获取mp3音频信息实例教程的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product