3

I am trying to get song name / artist name / song length / bitrate etc from a remote .mp3 file
such as http://shiro-desu.com/scr/11.mp3 .

I have tried getID3 script but from what i understand it doesn't work for remote files as i got this error: "Remote files are not supported - please copy the file locally first"

Also, this code:

<?php
$tag = id3_get_tag( "http://shiro-desu.com/scr/11.mp3" );
print_r($tag);
?>

did not work either.
"Fatal error: Call to undefined function id3_get_tag() in /home4/shiro/public_html/scr/index.php on line 2"

dimitris93
  • 4,155
  • 11
  • 50
  • 86

2 Answers2

1

As you haven't mentioned your error I am considering a common error case undefined function

The error you get (undefined function) means the ID3 extension is not enabled in your PHP configuration:

If you dont have Id3 extension file .Just check here for installation info.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Avinash Babu
  • 6,171
  • 3
  • 21
  • 26
  • thanks , i will install it, i didnt see any install links http://php.net/manual/en/function.id3-get-tag.php here , so i thought it didnt need any – dimitris93 Sep 21 '14 at 02:44
  • do you know how could i install the .zip i downloaded ? – dimitris93 Sep 21 '14 at 02:47
  • hmm im trying to install it on an actual bought server not WAMP, but i guess this will do – dimitris93 Sep 21 '14 at 02:52
  • 2
    http://designaeon.com/2012/07/read-mp3-tags-without-downloading-it/#_ this helped me a lot for anyone wondering, i used this with getID3 which you can find easily http://getid3.sourceforge.net/ – dimitris93 Sep 21 '14 at 03:49
  • https://web.archive.org/web/20160106095540/http://designaeon.com/2012/07/read-mp3-tags-without-downloading-it/ – vr_driver May 06 '20 at 01:16
  • I wrote up a full example: https://stephenmonro.wordpress.com/2020/05/06/get-mp3-id3-metadata-from-remote-urls/ – vr_driver May 06 '20 at 02:04
1

Firstly, I didn’t create this, I’ve just making it easy to understand with a full example.

You can read more of it here, but only because of archive.org. https://web.archive.org/web/20160106095540/http://designaeon.com/2012/07/read-mp3-tags-without-downloading-it/

To begin, download this library from here: http://getid3.sourceforge.net/

When you open the zip folder, you’ll see ‘getid3’. Save that folder in to your working folder.

Next, create a folder called “temp” in that working folder that the following script is going to be running from.

Basically, what it does is download the first 64k of the file, and then read the metadata from the file.

I enjoy a simple example. I hope this helps.

<?php

require_once("getid3/getid3.php");

$url_media = "http://example.com/myfile.mp3"

$a=getfileinfo($url_media);

echo"<pre>";

echo $a['tags']['id3v2']['album'][0] . "\n";  
echo $a['tags']['id3v2']['artist'][0] . "\n";  
echo $a['tags']['id3v2']['title'][0] . "\n";  
echo $a['tags']['id3v2']['year'][0] . "\n"; 
echo $a['tags']['id3v2']['year'][0] . "\n";  

echo "\n-----------------\n";

//print_r($a['tags']['id3v2']['album']);

echo "-----------------\n";

//print_r($a);

echo"</pre>";

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

?>
vr_driver
  • 1,867
  • 28
  • 39