【发布时间】: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 我现在提出的问题不同,但相似
-
但是试着让问题的标题更有意义,这样人们就能第一眼就知道了