php - 使用php读取远程mp3文件信息

标签 php mp3 id3lib getid3

我正在开发一个将从远程 URL 获取 mp3 详细信息的网站。我需要编写一个 cron 作业,以便它获取所有歌曲信息,例如文件名、路径、艺术家、流派、比特率、播放时间等,并将其放入数据库表中。

我尝试了 getid3 包,但是当一次获取多个 url 时速度非常慢,并且我得到了最大执行错误。

示例:

require_once('getid/getid3/getid3.php');

    $urls = array ('http://stackoverflow.com/test1.mp3','http://stackoverflow.com/test2.mp3''http://stackoverflow.com/test3.mp3');


    foreach($urls  as $ur){
      $mp3_det = getMp3Info( $ur );
      print_r ($mp3_det);

    }

    function getMp3Info ( $url ){
            if($url){
            /**********/
            $filename = tempnam('/tmp','getid3');
            if (file_put_contents($filename, file_get_contents($url, false, null, 0, 35000))) {
               if (require_once('getid/getid3/getid3.php')) {
                  $getID3 = new getID3;
                  $ThisFileInfo = $getID3->analyze($filename);
                  unlink($filename);
                  $bitratez = $ThisFileInfo[audio][bitrate] ? $ThisFileInfo[audio][bitrate] : '';
                  $headers = get_headers($url, 1);
                  if ((!array_key_exists("Content-Length", $headers))) { return false; }
                 // print $headers["Content-Length"];
                  $filesize= round($headers["Content-Length"]/1000);
                  $contentLengthKBITS=$filesize*8;
                  if ( $bitratez ){
                         $bitrate= round ( $bitratez/1000 );
                        $seconds=$contentLengthKBITS/$bitrate;
                        $playtime_mins = floor($seconds/60);
                        $playtime_secs = $seconds % 60;

                         if(strlen($playtime_secs)=='1'){$zero='0';}
                        $playtime_secs = $zero.$playtime_secs;
                        $playtime_string=$playtime_mins.':'.$playtime_secs;
                    }
                    else $playtime_string='0:00';

                 // echo '<pre>'.print_r($ThisFileInfo, true).'</pre>';
               }

               $bitrate = $bitrate ? $bitrate : 0;

                $ret = array();
                $ret['playtime'] = $playtime_string;
                $ret['filesize'] = $filesize;
                $ret['bitrate']  = $bitrate;

               return $ret;
            }
        }

最佳答案

您可以通过使用套接字连接并一次读取文件 block 并不断尝试分析文件来缩短执行时间。

由于 ID3 数据存储在 mp3 的开头,因此下载整个内容是没有意义的。我现在看到的最大问题是分析函数只接受文件名,而不是二进制数据(这就是您所拥有的)。因此,您必须更新代码,或者创建一个类似的函数来分析适用于您的二进制数据的函数。

关于php - 使用php读取远程mp3文件信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8785222/

相关文章:

php - 从数据库中转换乱码

java - 使用 FFT 比较两个音频文件是唯一的方法吗?

ios - 从 mp3 文件下载只需几秒钟

Android concat 2 音频(wav 或 mp3)文件

c++ - 在 C++ 应用程序中使用 DLL

php - preg_match 无法匹配从数据库中获取的字符串

php - 无法在html表中显示多个mysql查询结果

php - 基本 Stocktwits SSL