PHP: get metadata of remote .mp3 file

I'm looking for a function that gets the .mp3 file metadata from a URL (NOT a 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.

I am currently 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 loads 64KB from the url of the .mp3 file and then returns an array with metadata using the function getID3

(which works for local .mp3 files only

) and then removes the 64KB you downloaded earlier. The problem with this feature is that it is inherently too slow (downloads 64KB to .mp3, imagine 1000 mp3 files.)

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

+3


source to share


1 answer


This function loads 64KB from the URL of the .mp3 file, then returns an array with metadata using the getID3 function (which only works with local .mp3 files), and then removes the 64KB that was previously loaded. The problem with this feature is that it is inherently too slow (downloads 64KB to .mp3, imagine 1000 mp3 files.)

Yes, well, what do you suggest? How do you expect to receive data if you are not receiving data? It is not possible for a generic remote HTTP server to send you ID3 data. There is really no magic. Think about it.

What you are doing now is already pretty solid, except that it does not handle all versions of ID3 and will not work for files with more than 64KB of ID3 tags. I would do this to improve it to use multi-cURL.



Several PHP classes are available:

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

      

+1


source







All Articles