热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

很早之前在网上看到一个获取MP3音频信息的php类。如:播放时长、文件大小、文件编码等等,本文以详细过程讲解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[&#39;Encoding&#39;         ] = &#39;CBR&#39;;
            $this->mp3data[&#39;MPEG version&#39;     ] = $this->getnextbits(2);
            $this->mp3data[&#39;Layer Description&#39;] = $this->getnextbits(2);
            $this->mp3data[&#39;Protection Bit&#39;   ] = $this->getnextbits(1);
            $this->mp3data[&#39;Bitrate Index&#39;    ] = $this->getnextbits(4);
            $this->mp3data[&#39;Sampling Freq Idx&#39;] = $this->getnextbits(2);
            $this->mp3data[&#39;Padding Bit&#39;      ] = $this->getnextbits(1);
            $this->mp3data[&#39;Private Bit&#39;      ] = $this->getnextbits(1);
            $this->mp3data[&#39;Channel Mode&#39;     ] = $this->getnextbits(2);
            $this->mp3data[&#39;Mode Extension&#39;   ] = $this->getnextbits(2);
            $this->mp3data[&#39;Copyright&#39;        ] = $this->getnextbits(1);
            $this->mp3data[&#39;Original Media&#39;   ] = $this->getnextbits(1);
            $this->mp3data[&#39;Emphasis&#39;         ] = $this->getnextbits(1);
            $this->mp3data[&#39;Bitrate&#39;          ] = mp3file::bitratelookup($this->mp3data);
            $this->mp3data[&#39;Sampling Rate&#39;    ] = mp3file::samplelookup($this->mp3data);
            $this->mp3data[&#39;Frame Size&#39;       ] = mp3file::getframesize($this->mp3data);
            $this->mp3data[&#39;Length&#39;           ] = mp3file::getduration($this->mp3data,$this->tell2());
            $this->mp3data[&#39;Length mm:ss&#39;     ] = mp3file::seconds_to_mmss($this->mp3data[&#39;Length&#39;]);
            
            if ($this->mp3data[&#39;Bitrate&#39;      ]==&#39;bad&#39;     ||
                $this->mp3data[&#39;Bitrate&#39;      ]==&#39;free&#39;    ||
                $this->mp3data[&#39;Sampling Rate&#39;]==&#39;unknown&#39; ||
                $this->mp3data[&#39;Frame Size&#39;   ]==&#39;unknown&#39; ||
                $this->mp3data[&#39;Length&#39;     ]==&#39;unknown&#39;)
            $this->mp3data = array(&#39;Filesize&#39;=>$this->mp3data[&#39;Filesize&#39;], &#39;Encoding&#39;=>&#39;Unknown&#39;);
        }
        else
        {
            if(!isset($this->mp3data[&#39;Encoding&#39;]))
                $this->mp3data[&#39;Encoding&#39;] = &#39;Unknown&#39;;
        }
    }
    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 "
"; } } 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[&#39;ID3v2 Major version&#39;] = bindec(substr($bits,24,8)); $arr[&#39;ID3v2 Minor version&#39;] = bindec(substr($bits,32,8)); $arr[&#39;ID3v2 flags&#39; ] = bindec(substr($bits,40,8)); if (substr($bits,40,1)) $arr[&#39;Unsynchronisation&#39;]=true; if (substr($bits,41,1)) $arr[&#39;Extended header&#39;]=true; if (substr($bits,42,1)) $arr[&#39;Experimental indicator&#39;]=true; if (substr($bits,43,1)) $arr[&#39;Footer present&#39;]=true; $size = ""; for($i=0; $i<4; $i++) { $this->getnextbit();//skip this bit, should be 0 $size.= $this->getnextbits(7); } $arr[&#39;ID3v2 Tags Size&#39;]=bindec($size);//now the size is in bytes; if ($arr[&#39;ID3v2 Tags Size&#39;] - $this->blockmax>0) { fseek($this->fd, $arr[&#39;ID3v2 Tags Size&#39;]+10 ); $this->prefetchblock(); if (isset($arr[&#39;Footer present&#39;]) && $arr[&#39;Footer present&#39;]) { for($i=0; $i<10; $i++) $this->getnextbyte();//10 footer bytes } } else { for($i=0; $i<$arr[&#39;ID3v2 Tags Size&#39;]; $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[&#39;Layer Description&#39;]==&#39;11&#39;); } public static function is_layer2(&$mp3) { return ($mp3[&#39;Layer Description&#39;]==&#39;10&#39;); } public static function is_layer3(&$mp3) { return ($mp3[&#39;Layer Description&#39;]==&#39;01&#39;); } public static function is_mpeg10(&$mp3) { return ($mp3[&#39;MPEG version&#39;]==&#39;11&#39;); } public static function is_mpeg20(&$mp3) { return ($mp3[&#39;MPEG version&#39;]==&#39;10&#39;); } public static function is_mpeg25(&$mp3) { return ($mp3[&#39;MPEG version&#39;]==&#39;00&#39;); } public static function is_mpeg20or25(&$mp3) { return ($mp3[&#39;MPEG version&#39;]{1}==&#39;0&#39;); } //----------------------------------------------------------------------------- public static function bitratelookup(&$mp3) { //bits V1,L1 V1,L2 V1,L3 V2,L1 V2,L2&L3 $array = array(); $array[&#39;0000&#39;]=array(&#39;free&#39;,&#39;free&#39;,&#39;free&#39;,&#39;free&#39;,&#39;free&#39;); $array[&#39;0001&#39;]=array( &#39;32&#39;, &#39;32&#39;, &#39;32&#39;, &#39;32&#39;, &#39;8&#39;); $array[&#39;0010&#39;]=array( &#39;64&#39;, &#39;48&#39;, &#39;40&#39;, &#39;48&#39;, &#39;16&#39;); $array[&#39;0011&#39;]=array( &#39;96&#39;, &#39;56&#39;, &#39;48&#39;, &#39;56&#39;, &#39;24&#39;); $array[&#39;0100&#39;]=array( &#39;128&#39;, &#39;64&#39;, &#39;56&#39;, &#39;64&#39;, &#39;32&#39;); $array[&#39;0101&#39;]=array( &#39;160&#39;, &#39;80&#39;, &#39;64&#39;, &#39;80&#39;, &#39;40&#39;); $array[&#39;0110&#39;]=array( &#39;192&#39;, &#39;96&#39;, &#39;80&#39;, &#39;96&#39;, &#39;48&#39;); $array[&#39;0111&#39;]=array( &#39;224&#39;, &#39;112&#39;, &#39;96&#39;, &#39;112&#39;, &#39;56&#39;); $array[&#39;1000&#39;]=array( &#39;256&#39;, &#39;128&#39;, &#39;112&#39;, &#39;128&#39;, &#39;64&#39;); $array[&#39;1001&#39;]=array( &#39;288&#39;, &#39;160&#39;, &#39;128&#39;, &#39;144&#39;, &#39;80&#39;); $array[&#39;1010&#39;]=array( &#39;320&#39;, &#39;192&#39;, &#39;160&#39;, &#39;160&#39;, &#39;96&#39;); $array[&#39;1011&#39;]=array( &#39;352&#39;, &#39;224&#39;, &#39;192&#39;, &#39;176&#39;, &#39;112&#39;); $array[&#39;1100&#39;]=array( &#39;384&#39;, &#39;256&#39;, &#39;224&#39;, &#39;192&#39;, &#39;128&#39;); $array[&#39;1101&#39;]=array( &#39;416&#39;, &#39;320&#39;, &#39;256&#39;, &#39;224&#39;, &#39;144&#39;); $array[&#39;1110&#39;]=array( &#39;448&#39;, &#39;384&#39;, &#39;320&#39;, &#39;256&#39;, &#39;160&#39;); $array[&#39;1111&#39;]=array( &#39;bad&#39;, &#39;bad&#39;, &#39;bad&#39;, &#39;bad&#39;, &#39;bad&#39;); $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[&#39;Bitrate Index&#39;]][$whichcolumn])) return $array[$mp3[&#39;Bitrate Index&#39;]][$whichcolumn]; else return "bad"; } //----------------------------------------------------------------------------- public static function samplelookup(&$mp3) { //bits MPEG1 MPEG2 MPEG2.5 $array = array(); $array[&#39;00&#39;] =array(&#39;44100&#39;,&#39;22050&#39;,&#39;11025&#39;); $array[&#39;01&#39;] =array(&#39;48000&#39;,&#39;24000&#39;,&#39;12000&#39;); $array[&#39;10&#39;] =array(&#39;32000&#39;,&#39;16000&#39;,&#39;8000&#39;); $array[&#39;11&#39;] =array(&#39;res&#39;,&#39;res&#39;,&#39;res&#39;); $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[&#39;Sampling Freq Idx&#39;]][$whichcolumn])) return $array[$mp3[&#39;Sampling Freq Idx&#39;]][$whichcolumn]; else return &#39;unknown&#39;; } //----------------------------------------------------------------------------- public static function getframesize(&$mp3) { if ($mp3[&#39;Sampling Rate&#39;]>0) { return ceil((144 * $mp3[&#39;Bitrate&#39;]*1000)/$mp3[&#39;Sampling Rate&#39;]) + $mp3[&#39;Padding Bit&#39;]; } return &#39;unknown&#39;; } //----------------------------------------------------------------------------- public static function getduration(&$mp3,$startat) { if ($mp3[&#39;Bitrate&#39;]>0) { $KBps = ($mp3[&#39;Bitrate&#39;]*1000)/8; $datasize = ($mp3[&#39;Filesize&#39;] - ($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 ); } }

调用

get_metadata();
   return $a[&#39;Length mm:ss&#39;] ? $a[&#39;Length mm:ss&#39;] : 0;
}
function mp3Info($file) {
   $m = new mp3file($file);
   return $m->get_metadata();
}
$_time = mp3Time(&#39;Beyond-情人.mp3&#39;);
echo "歌曲时间长:{$_time}\n";
$_info = mp3Info(&#39;Beyond-情人.mp3&#39;);
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音频信息实例教程的详细内容,更多请关注 第一PHP社区 其它相关文章!


推荐阅读
  • Linux环境变量$PATH的作用及使用方法
    本文介绍了Linux环境变量$PATH的作用及使用方法。$PATH是一个由多个目录组成的变量,用冒号分隔。当执行一个指令时,系统会按照$PATH定义的目录顺序搜索同名的可执行文件,如果有多个同名指令,则先找到的会被执行。通过设置$PATH变量,可以在任何地方执行指令,无需输入绝对路径。 ... [详细]
  • Windows7企业版怎样存储安全新功能详解
    本文介绍了电脑公司发布的GHOST WIN7 SP1 X64 通用特别版 V2019.12,软件大小为5.71 GB,支持简体中文,属于国产软件,免费使用。文章还提到了用户评分和软件分类为Win7系统,运行环境为Windows。同时,文章还介绍了平台检测结果,无插件,通过了360、腾讯、金山和瑞星的检测。此外,文章还提到了本地下载文件大小为5.71 GB,需要先下载高速下载器才能进行高速下载。最后,文章详细解释了Windows7企业版的存储安全新功能。 ... [详细]
  • PHP组合工具以及开发所需的工具
    本文介绍了PHP开发中常用的组合工具和开发所需的工具。对于数据分析软件,包括Excel、hihidata、SPSS、SAS、MARLAB、Eview以及各种BI与报表工具等。同时还介绍了PHP开发所需的PHP MySQL Apache集成环境,包括推荐的AppServ等版本。 ... [详细]
  • asp中如何嵌入python的简单介绍
    本文目录一览:1、如何在IIS中执行Python脚本 ... [详细]
  • vb.net不用多线程如何同时运行两个过程?不用多线程?即使用多线程,也不会是“同时”执行,题主只要略懂一些计算机编译原理就能明白了。不用多线程更不可能让两个过程同步执行了。不过可 ... [详细]
  • Win10 64位旗舰版的优势及特点详解
    本文详细介绍了Win10 64位旗舰版的优势及特点,包括更安全的源安装盘、永久激活方式、稳定性和硬件驱动的集成,以及人性化的维护工具和分区功能。通过阅读本文,您将了解到Win10 64位旗舰版相比其他版本的优势和特点。 ... [详细]
  • 现在学vb6还靠得住么?语言只是工具,关键是思想。程序=算法+数据结构。除了汇编,其他语言都靠不住。随着时代的进步,很多语言跟不上开发的要求。从面向过程到面向对象,与其说是思想的进步,不如说是为了适应高速开发。除了底层汇编语言,还有那些能适应高速开发的语言。每种语言都是很有趣的。 ... [详细]
  • 服务器上的操作系统有哪些,如何选择适合的操作系统?
    本文介绍了服务器上常见的操作系统,包括系统盘镜像、数据盘镜像和整机镜像的数量。同时,还介绍了共享镜像的限制和使用方法。此外,还提供了关于华为云服务的帮助中心,其中包括产品简介、价格说明、购买指南、用户指南、API参考、最佳实践、常见问题和视频帮助等技术文档。对于裸金属服务器的远程登录,本文介绍了使用密钥对登录的方法,并提供了部分操作系统配置示例。最后,还提到了SUSE云耀云服务器的特点和快速搭建方法。 ... [详细]
  • 本文详细介绍了如何创建和使用VUE uni-app开发环境,包括通过HBuilderX可视化界面和通过vue-cli命令执行的方法。文章内容简单清晰,易于学习与理解。通过学习本文,读者可以深入了解VUE uni-app开发环境,并通过实践验证掌握具体的使用情况。编程笔记将为读者推送更多相关知识点的文章,欢迎关注! ... [详细]
  • 本文介绍了一个从入门到高手的VB.NET源代码,通过学习这些源代码,可以在21天内成为VB.NET高手。文章提供了下载地址,并提醒读者加入作者的QQ群和收藏作者的博客。 ... [详细]
  • 全面介绍Windows内存管理机制及C++内存分配实例(四):内存映射文件
    本文旨在全面介绍Windows内存管理机制及C++内存分配实例中的内存映射文件。通过对内存映射文件的使用场合和与虚拟内存的区别进行解析,帮助读者更好地理解操作系统的内存管理机制。同时,本文还提供了相关章节的链接,方便读者深入学习Windows内存管理及C++内存分配实例的其他内容。 ... [详细]
  • GTX1070Ti显卡怎么样?GTX1070Ti显卡首发图赏+参数解读与拆解图
    先来简单回顾一下今年的显卡市场,nvidia自从发布了帕斯卡架构新品之后,可以说是一直都主宰着高端游戏显卡市场,虽说amd也憋了一个hbm2的vega64出来,然而即使是最高贵的水 ... [详细]
  • 本文介绍了禅道作为一款国产开源免费的测试管理工具的特点和功能,并提供了禅道的搭建和调试方法。禅道是一款B/S结构的项目管理工具,可以实现组织管理、后台管理、产品管理、项目管理和测试管理等功能。同时,本文还介绍了其他软件测试相关工具,如功能自动化工具和性能自动化工具,以及白盒测试工具的使用。通过本文的阅读,读者可以了解禅道的基本使用方法和优势,从而更好地进行测试管理工作。 ... [详细]
  • VBA操作Excel之设置单元格属性
    VBA操作Excel简介一、VBA读写Excel文件二、VBA设置单元格属性三、VBA弹出输入和输出窗口参考文档一、VBA读写Excel文件VBA简介及打开Excel文件方法见VB ... [详细]
  • 如题如示,在网上查了下c#直接操作ppt的例子,但都只是很简单的写了下打开PPT插入标题插入一个图表等,但是都没有更具体的有如何可以直接更改PPT图表的数据源数据的例子。我现在的需求是,我有一个P ... [详细]
author-avatar
手机用户2602929277
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有