来源:flash,as,js兴趣爱好者php获得mp3文件总时间,php获得音频文件信息,php获得mp3文件信息    php获得mp3文件总时间,php获得音频文件信息,php获得mp3文件信息
   新建mp3file.class.php文件,然后新建mp3.php

//mp3file.class.php内的代码 <?php class 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 ); } } //mp3.php内的代码 <?php include_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('3.mp3'); echo '<meta charset="UTF-8">'; echo "歌曲时间长:".$_time.'<br />'; $_info = mp3Info('3.mp3'); print_r($_info); ?>

php获得mp3文件总时间,php获得音频文件信息,php获得mp3文件信息

转载于:https://blog.51cto.com/3096063/1357943

php获得mp3文件总时间,php获得音频文件信息,php获得mp3文件信息相关推荐

  1. linux读取文件修改时间函数,Linux服务器编程之utime()函数修改文件存取时间

    Linux服务器编程之utime()函数修改文件存取时间 C语言utime()函数:修改文件的存取时间和更改时间 头文件: #include #include 定义函数: int utime(cons ...

  2. C#统计酷狗播放列表里歌曲播放的总时间

    手机音乐播发器里有总文件总时间的统计,在酷狗播放器里找了一下没找到.那我想知道这些歌曲的总时间,该怎么办? 其实很简单,稍稍动动手,就能找到答案! 请参考如下步骤: 第一步,保存播放列表,把里面的歌曲 ...

  3. MPEG简介 + 如何计算 CBR/VBR MP3 的播放时间

    [此文目的]  1.  了解 MPEG相关知识  2.  了解 MP3 的常见术语含义  3.  详解 VBR MP3的帧头格式及含义  4.  搞懂如何去计算 CBR和 VBR的 MP3文件的播放时 ...

  4. linux 禁止文件修改时间,linux 修改文件的时间属性

    二.修改文件时间 创建文件我们可以通过touch来创建.同样,我们也可以使用touch来修改文件时间.touch的相关参数如下: -a : 仅修改access time. -c : 仅修改时间,而不建 ...

  5. linux cp保留时间,linux – cp -p会保留一些文件的时间但不是全部吗?

    我在一台机器上有一个源目录,其中有一堆文件,都具有相同的权限,我想复制到另一个目录,通过SMB挂载. [tmark@dexter JR09141045 roche_454_transfer]$ls - ...

  6. linux更新文件名时间,Linux文件的时间及修改命令touch-linux修改文件名

    Linux文件的时间及修改命令touch Linux系统中的文件主要有三个时间参数,他们分别是修改时间(modification time,mtime).状态时间(status time,ctime) ...

  7. 音频文件格式怎么转换html,mp3格式转换 如何转换音频文件|手机录音转换mp3格式...

    昨天晚上翻来覆去睡不着的时候随机播放软件推荐的歌曲时听到了一首我自己觉得很好听的歌,蔡健雅的一首<被驯服的象>,我也推荐给大家听一下.今天早上上班就发现太阳已经升起来了哦,正愁没衣服穿了. ...

  8. 在单缓冲区和双缓冲区结构下,读入并分析完该文件的时间分别是

    链接:https://www.nowcoder.com/questionTerminal/5a9eff01452240a7a97495cef08ad3f6?toCommentId=398027 来源: ...

  9. Android 节操视频播放器jiecaovideoplayer自定义播放音频使用:屏蔽全屏按钮,增加倒计时,当前时间/总时间

    一.屏蔽全屏按钮 找到JCVideoPlayerStandard.java文件中的代码: private void fixAudio() {if (SrcType.equalsIgnoreCase(& ...

  10. 将文件名和文件修改时间批量输出至Excel中

    操作系统:Windows10 软件版本:Office2019 1.打开一个空的Excel表格,Alt+F11,双击Sheet1进入代码编辑,输入代码如下: Sub main()ff = Dir(&qu ...

最新文章

  1. 美国多所高校宣称9月线下开学,纽约大学教授:请停止妄想!
  2. mysql创建索引语句
  3. 利用网站模板创建子网站
  4. 14寸笔记本电脑推荐_2020笔记本电脑推荐(小米篇)
  5. Enterprise Libary 2 Hands-on-Lab 发布了
  6. dockerfile COPY
  7. PHP连接mysql8.0出错“SQLSTATE[HY000] [2054] The server requested authentication method unknow........
  8. POJ 2686 Traveling by Stagecoach 壮压DP
  9. php 如何下载,php的包怎么下载
  10. spring 的3种常用的注入方式
  11. wacom影拓系列数位板驱动
  12. win10如何打开摄像头_解决win10相机无法使用,相机崩溃问题
  13. 远程桌面连接(远程控制另一电脑)的详细步骤
  14. web 前端签名插件_手写签名插件—jSignature
  15. 高得地图 +数据绑定(databinding) + BaseQuickAdapter 自定义地图选点!
  16. 手把手介绍Manjaro中添加Google输入法---亲身测试,成功安装
  17. Linux-IO全整理:BIO/NIO/IO多路复用解析
  18. python编辑器windows_windows下sublime Text 3 做Python编辑器 详细配置
  19. 关于xftp和xshell 软件评估期已过的解决办法
  20. 腾讯的这个框架火了!

热门文章

  1. python ndarray转binary_Python 实现Image和Ndarray互相转换
  2. python将txt读入矩阵_python读入txt数据,并转成矩阵
  3. 计算机设备管理程序在哪,设备管理器在哪里查找?如何打开?
  4. Ubuntu-vim 命令
  5. vue element ui 样式修改无效
  6. PTA 程序设计天梯赛(61~80题)
  7. 【安装包】XMind-ZEN-Update-2019-for-Windows-64bit-9.2.1
  8. android studio 如何导入 RecyclerView
  9. python中的import
  10. 机器学习笔记(二)线性回归模型实现