dongzheng7165 2012-01-09 07:38
浏览 128

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

I am working on a site which will fetch mp3 details from a remote url. I need to write a cron job so that it gets all the song information such as the file name, path, artist, genre, bitrate, playing time, etc and put it in a database table.

I tried getid3 package, but this is very slow when fetching more than one url at a time and I get maximum execution error.

Example:

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;
            }
        }
  • 写回答

2条回答 默认 最新

  • dry69034 2012-01-09 07:46
    关注

    MP3 files comes along with some kind of Meta Data almost same way to some other binary file formats. They are in ID tags. There are many versions of ID tags, like ID3 or ID4 tags. Now, there is easy way to extract IDv3 tag informations supplied along with MP3 file, through PHP.

    You need to download some library in PHP from sourceforge, like getID3. This way you can extract artist name, genre, duration, length, size etc information from an mp3 file. IDv4 contains additional informations such as album art.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度