很早之前在网上看到一个获取MP3音频信息的php类。如:播放时长、文件大小、文件编码等等,本文以详细过程讲解php获取mp3音频信息的方法。,php获取mp3音频信息实例教程
|
php获取mp3音频信息 很早之前在网上看到一个获取 MP3 音频信息的 php 类。如:播放时长、文件大小、文件编码等等 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 "";
//echo "";
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 "调用 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:15
Array
(
[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音频信息实例教程的详细内容,更多请关注 模板之家(www.mb5.com.cn) 其它相关文章! |
