0

0

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

coldplay.xixi

coldplay.xixi

发布时间:2020-06-15 16:34:19

|

2617人浏览过

|

来源于aiti.fun

转载

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

php获取mp3音频信息

很早之前在网上看到一个获取 MP3 音频信息的 php 类。如:播放时长、文件大小、文件编码等等

微信公众平台开发入门教程 中文WORD版
微信公众平台开发入门教程 中文WORD版

由于微信的大热,为了更好的方便使用微信的用户查询一些信息,这篇文章是入门级的微信公众平台开发教程,需要的朋友可以参考下 这篇入门教程将引导你完成如下任务: 创建百度云平台应用启用微信公众平台开发模式获取订阅、文字、图片、语音、视频消息回复文本、图文及音乐消息程序开发

下载
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 "
"; } } 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 ); } }

调用

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速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
php文件怎么打开
php文件怎么打开

打开php文件步骤:1、选择文本编辑器;2、在选择的文本编辑器中,创建一个新的文件,并将其保存为.php文件;3、在创建的PHP文件中,编写PHP代码;4、要在本地计算机上运行PHP文件,需要设置一个服务器环境;5、安装服务器环境后,需要将PHP文件放入服务器目录中;6、一旦将PHP文件放入服务器目录中,就可以通过浏览器来运行它。

2892

2023.09.01

php怎么取出数组的前几个元素
php怎么取出数组的前几个元素

取出php数组的前几个元素的方法有使用array_slice()函数、使用array_splice()函数、使用循环遍历、使用array_slice()函数和array_values()函数等。本专题为大家提供php数组相关的文章、下载、课程内容,供大家免费下载体验。

1733

2023.10.11

php反序列化失败怎么办
php反序列化失败怎么办

php反序列化失败的解决办法检查序列化数据。检查类定义、检查错误日志、更新PHP版本和应用安全措施等。本专题为大家提供php反序列化相关的文章、下载、课程内容,供大家免费下载体验。

1565

2023.10.11

php怎么连接mssql数据库
php怎么连接mssql数据库

连接方法:1、通过mssql_系列函数;2、通过sqlsrv_系列函数;3、通过odbc方式连接;4、通过PDO方式;5、通过COM方式连接。想了解php怎么连接mssql数据库的详细内容,可以访问下面的文章。

1099

2023.10.23

php连接mssql数据库的方法
php连接mssql数据库的方法

php连接mssql数据库的方法有使用PHP的MSSQL扩展、使用PDO等。想了解更多php连接mssql数据库相关内容,可以阅读本专题下面的文章。

1546

2023.10.23

html怎么上传
html怎么上传

html通过使用HTML表单、JavaScript和PHP上传。更多关于html的问题详细请看本专题下面的文章。php中文网欢迎大家前来学习。

1277

2023.11.03

PHP出现乱码怎么解决
PHP出现乱码怎么解决

PHP出现乱码可以通过修改PHP文件头部的字符编码设置、检查PHP文件的编码格式、检查数据库连接设置和检查HTML页面的字符编码设置来解决。更多关于php乱码的问题详情请看本专题下面的文章。php中文网欢迎大家前来学习。

1649

2023.11.09

php文件怎么在手机上打开
php文件怎么在手机上打开

php文件在手机上打开需要在手机上搭建一个能够运行php的服务器环境,并将php文件上传到服务器上。再在手机上的浏览器中输入服务器的IP地址或域名,加上php文件的路径,即可打开php文件并查看其内容。更多关于php相关问题,详情请看本专题下面的文章。php中文网欢迎大家前来学习。

1309

2023.11.13

c++ 根号
c++ 根号

本专题整合了c++根号相关教程,阅读专题下面的文章了解更多详细内容。

70

2026.01.23

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP课程
PHP课程

共137课时 | 9.4万人学习

JavaScript ES5基础线上课程教学
JavaScript ES5基础线上课程教学

共6课时 | 11.2万人学习

PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 0.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号