search
HomeBackend DevelopmentPHP TutorialExample tutorial of obtaining mp3 audio information in php

Example tutorial of obtaining mp3 audio information in php

php Get mp3 audio information

A long time ago I saw a php class on the Internet for getting MP3 audio information . Such as: playback duration, file size, file encoding, etc.

<?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[&#39;Filesize&#39;] = filesize($filename);
        $this->fd = fopen($filename,&#39;rb&#39;);
        $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[&#39;Encoding&#39;] = &#39;VBR&#39;;
            $iscbrmp3=false;
        }
        else if ($this->startswithpk())
        {
            $this->mp3data[&#39;Encoding&#39;] = &#39;Unknown&#39;;
            $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 "<!--".$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[&#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 );
    }
}

Call

<?php
include_once &#39;mp3file.class.php&#39;;
function mp3Time($file) {
   $m = new mp3file($file);
   $a = $m->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);

Output information

→ 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
)

Recommended tutorial: "php video tutorial"

The above is the detailed content of Example tutorial of obtaining mp3 audio information in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:aiti.fun. If there is any infringement, please contact admin@php.cn delete
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function