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

发布于 2024-12-25 15:51:03 字数 2256 浏览 3 评论 0原文

我正在开发一个将从远程 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;
            }
        }

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;
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一向肩并 2025-01-01 15:51:03

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

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

You may be able to help the execution time by using a socket connection and reading in chunks of the file at a time, and continuously trying to analyze the file.

Since ID3 data is stored in the beginning of the mp3, there is no point in downloading the entire thing. THe biggest problem I see right now is that the analyze function only takes a filename, not binary data (which is what you would have). So, you would have to either update the code, or make a similar function to analyze that works with your binary data.

不弃不离 2025-01-01 15:51:03

MP3 文件附带某种元数据,其方式与其他一些二进制文件格式几乎相同。它们位于 ID 标签中。 ID 标签有很多版本,例如 ID3 或 ID4 标签。现在,有一种简单的方法可以通过 PHP 提取随 MP3 文件提供的 IDv3 标签信息。

你需要从sourceforge下载一些PHP库,比如getID3。这样您就可以从 mp3 文件中提取艺术家姓名、流派、持续时间、长度、大小等信息。 IDv4 包含专辑封面等附加信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文