PHP:获取远程.mp3文件的元数据

3
我正在寻找一个函数,可以从URL(而不是我的服务器上的本地.mp3文件)获取.mp3文件的元数据。
此外,我不想在我的服务器上安装http://php.net/manual/en/id3.installation.php或任何类似的东西。
我正在寻找一个独立的函数。

现在我正在使用这个函数:

<?php
function getfileinfo($remoteFile) 
{ 
    $url=$remoteFile;
    $uuid=uniqid("designaeon_", true);
    $file="../temp/".$uuid.".mp3";
    $size=0;
    $ch = curl_init($remoteFile);
    //==============================Get Size==========================//
    $contentLength = 'unknown';
    $ch1 = curl_init($remoteFile);
    curl_setopt($ch1, CURLOPT_NOBODY, true);
    curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch1, CURLOPT_HEADER, true);
    curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here)
    $data = curl_exec($ch1);
    curl_close($ch1);
    if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
    $contentLength = (int)$matches[1];
    $size=$contentLength;
    }
    //==============================Get Size==========================//                 
    if (!$fp = fopen($file, "wb")) {
    echo 'Error opening temp file for binary writing';
    return false;
    } else if (!$urlp = fopen($url, "r")) {
    echo 'Error opening URL for reading';
    return false;
    }
    try {
    $to_get = 65536; // 64 KB
    $chunk_size = 4096; // Haven't bothered to tune this, maybe other values would work better??
    $got = 0; $data = null;

    // Grab the first 64 KB of the file
    while(!feof($urlp) && $got < $to_get) {  $data = $data . fgets($urlp, $chunk_size);  $got += $chunk_size;  }  fwrite($fp, $data);  // Grab the last 64 KB of the file, if we know how big it is.  if ($size > 0) {
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RESUME_FROM, $size - $to_get);
    curl_exec($ch);

    // Now $fp should be the first and last 64KB of the file!!               
    @fclose($fp);
    @fclose($urlp);
    } catch (Exception $e) {
    @fclose($fp);
    @fclose($urlp);
    echo 'Error transfering file using fopen and cURL !!';
    return false;
    }
    $getID3 = new getID3;
    $filename=$file;
    $ThisFileInfo = $getID3->analyze($filename);
    getid3_lib::CopyTagsToComments($ThisFileInfo);
    unlink($file);
    return $ThisFileInfo;
}
?>

这个函数从一个 .mp3 文件的 URL 中下载 64KB 的数据,然后使用 getID3 函数返回包含元数据的数组(该函数仅适用于本地 .mp3 文件),最后删除之前下载的 64KB 数据。该函数的问题在于其速度太慢了(每个 .mp3 文件下载 64KB 的数据,想象一下 1000 个 .mp3 文件)。
为了使我的问题更清晰:我需要一个快速的独立函数,可以读取远程 URL .mp3 文件的元数据。

2
所以你的意思是你不想使用专门为此构建的库,而是希望某个用户在不需要安装其他软件包的情况下重新构建了该库? - Ohgodwhy
再次提问???https://dev59.com/YYPba4cB1Zd3GeqPuJWL - Sujit Agarwal
1
这个库需要在每台服务器上安装,是吗?这意味着如果我更换服务器,我将不得不从头开始安装这个库。假设我制作了一个需要使用此库的 .php 网站,然后我想要出售它。我将不得不单独教每个买家如何在他们的服务器上安装这个库,以便我的 .php 在他们的服务器上运行。你明白我的意思吗? - dimitris93
@SujitAgarwal,我现在提出的问题不同,但类似。 - dimitris93
但请尽量让问题标题更有意义,这样人们一眼就能了解问题的大致内容。 - Sujit Agarwal
1个回答

1
这个函数从.mp3文件的URL下载64KB,然后使用getID3功能(仅适用于本地.mp3文件)返回元数据数组,最后删除先前下载的64KB。该函数的问题在于它的本质使其太慢了(每个.mp3下载64KB,想象1000个mp3文件)。你有什么建议?如果不获取数据,你怎么能获得数据?没有办法让通用远程HTTP服务器向您发送ID3数据。真的,没有魔法。想想看。现在你正在做的已经很不错了,只是它不能处理所有版本的ID3,并且对于具有超过64KB ID3标签的文件无效。我要改进它的方法是使用multi-cURL。有几个可用的PHP类使这更容易:

https://github.com/jmathai/php-multi-curl

$mc = EpiCurl::getInstance();
$results[] = $mc->addUrl(/* Your stream URL here /*); // Run this in a loop, 10 at a time or so

foreach ($results as $result) {
    // Do something with the data.
}

我明白你的意思。我不理解EpiCurl类的用途,它是用来处理不同版本的ID3吗?还是你的意思是$results[]是一个包含我正在尝试获取元数据的所有URL的数组? - dimitris93
这会同时下载10个URL的元数据吗? - dimitris93
@Shiro 是的,那个类的目的是同时发出多个HTTP请求。这将允许您一次获取10个左右的文件。 - Brad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接