简体   繁体   English

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

[英]PHP: Get metadata of a remote .mp3 file

I am looking for a function that gets the metadata of a .mp3 file from a URL (NOT local .mp3 file on my server). 我正在寻找一个从URL获取.mp3文件元数据的函数(我服务器上的本地.mp3文件)。
Also, I don't want to install http://php.net/manual/en/id3.installation.php or anything similar to my server. 另外,我不想安装http://php.net/manual/en/id3.installation.php或类似于我的服务器的任何东西。
I am looking for a standalone function. 我正在寻找一个独立的功能。

Right now i am using this function: 现在我正在使用这个功能:

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

This function downloads 64KB from a URL of an .mp3 file, then returns the array with the metadata by using getID3 function (which works on local .mp3 files only ) and then deletes the 64KB's previously downloaded. 此函数从.mp3文件的URL下载64KB,然后使用getID3函数(仅适用于local .mp3 files only )返回包含元数据的数组,然后删除先前下载的64KB。 Problem with this function is that it is way too slow from its nature (downloads 64KB's per .mp3, imagine 1000 mp3 files.) 这个函数的问题在于它的性质太慢了(每个.mp3下载64KB,想象1000个mp3文件。)

To make my question clear : I need a fast standalone function that reads metadata of a remote URL .mp3 file. 为了使我的问题清楚:我需要一个快速独立的函数来读取远程URL .mp3文件的元数据。

This function downloads 64KB from a URL of an .mp3 file, then returns the array with the metadata by using getID3 function (which works on local .mp3 files only) and then deletes the 64KB's previously downloaded. 此函数从.mp3文件的URL下载64KB,然后使用getID3函数(仅适用于本地.mp3文件)返回包含元数据的数组,然后删除先前下载的64KB。 Problem with this function is that it is way too slow from its nature (downloads 64KB's per .mp3, imagine 1000 mp3 files.) 这个函数的问题在于它的性质太慢了(每个.mp3下载64KB,想象1000个mp3文件。)

Yeah, well what do you propose? 是的,你有什么建议? How do you expect to get data if you don't get data? 如果您没有获得数据,您希望如何获得数据? There is no way to have a generic remote HTTP server send you that ID3 data. 没有办法让通用远程HTTP服务器向您发送ID3数据。 Really, there is no magic. 真的,没有魔力。 Think about it. 想一想。

What you're doing now is already pretty solid, except that it doesn't handle all versions of ID3 and won't work for files with more than 64KB of ID3 tags. 你现在正在做的事情已经非常可靠,除了它不能处理所有版本的ID3,并且不适用于ID3标签超过64KB的文件。 What I would do to improve it to is to use multi-cURL. 我要做的是改进它是使用multi-cURL。

There are several PHP classes available that make this easier: 有几个PHP类可以使这更容易:

https://github.com/jmathai/php-multi-curl 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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM