简体   繁体   中英

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).
Also, I don't want to install http://php.net/manual/en/id3.installation.php or anything similar to my server.
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. Problem with this function is that it is way too slow from its nature (downloads 64KB's per .mp3, imagine 1000 mp3 files.)

To make my question clear : I need a fast standalone function that reads metadata of a remote URL .mp3 file.

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. Problem with this function is that it is way too slow from its nature (downloads 64KB's per .mp3, imagine 1000 mp3 files.)

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. 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. What I would do to improve it to is to use multi-cURL.

There are several PHP classes available that make this easier:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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