【问题标题】:PHP: Get metadata of a remote .mp3 filePHP:获取远程 .mp3 文件的元数据
【发布时间】:2014-09-24 18:55:51
【问题描述】:

我正在寻找一个从 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 函数(适用于 local .mp3 files only)返回包含元数据的数组,然后删除之前下载的 64KB。 这个函数的问题在于它本身的速度太慢了(每个 .mp3 下载 64KB,想象一下 1000 个 mp3 文件。)

为了明确我的问题:我需要一个快速的独立函数来读取远程 URL .mp3 文件的元数据。

【问题讨论】:

  • 那么你的意思是你不想使用专门为此构建的库,而是希望某个地方的某个用户已经重建了库而不需要额外的包安装?跨度>
  • 这个库需要在每台服务器上安装,不是吗?这意味着如果我更改服务器,我将不得不从头开始安装这个库。假设我做了一个需要使用这个库的 .php 网站,然后我想卖掉它。我必须单独教每个买家如何在他们的服务器上安装这个库,以便我的 .php 可以在他们的服务器上运行?你明白我的意思吗?
  • @SujitAgarwal 我现在提出的问题不同,但相似
  • 但是试着让问题的标题更有意义,这样人们就能第一眼就知道了

标签: php mp3 metadata


【解决方案1】:

此函数从 .mp3 文件的 URL 下载 64KB,然后使用 getID3 函数(仅适用于本地 .mp3 文件)返回包含元数据的数组,然后删除之前下载的 64KB。这个函数的问题在于它本身的速度太慢了(每个 .mp3 下载 64KB,想象一下 1000 个 mp3 文件。)

是的,你有什么建议?如果你没有得到数据,你期望如何得到数据?无法让通用远程 HTTP 服务器向您发送 ID3 数据。真的,没有魔法。想一想。

您现在所做的已经非常可靠了,只是它不能处理所有版本的 ID3,并且不适用于 ID3 标签超过 64KB 的文件。我会做的改进是使用多 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?
  • 这样会一次性下载10个url的元数据吗?
  • @Shiro 是的,该类的重点是同时发出多个 HTTP 请求。这将允许您一次获取 10 个左右的文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-01
  • 2011-06-10
  • 2011-01-19
  • 2021-09-11
  • 1970-01-01
相关资源
最近更新 更多