External File Support

What features would you like to see in future versions of getID3() ?
Post Reply
Windoves
User
Posts: 9
Joined: Sun Jul 03, 2011 6:22 am
Are you a spambot?: no

External File Support

Post by Windoves » Mon Aug 01, 2011 5:22 pm

So external file support is kind of popular. To help out my wish, I have written a patch that calculates the seconds of the file without downloading the entire thing.

Pros:
- Users can get info from remote files without downloading them entirely.
Cons:
- The amount downloaded (currently 300000) is a lot. It can even be more, I would say the safe limit is 1,000,000.

Code: Select all

<?php

$url='http://getid3.org/demo/test.mp3'; // define the remote mp3 file

if($url){
$filename = tempnam('/tmp','getid3');
if (file_put_contents($filename, file_get_contents($url, false, null, 0, 300000))) {
   if (require_once('getid3/getid3.php')) {
      $getID3 = new getID3;
      $ThisFileInfo = $getID3->analyze($filename);
   }
   unlink($filename);
}
}

$bitratez=$ThisFileInfo[audio][bitrate]; // get the bitrate from the audio file

$headers = get_headers($url, 1); // Get the headers from the remote file
            if ((!array_key_exists("Content-Length", $headers))) { return false; } // Get the content length from the remote file
            $filesize= round($headers["Content-Length"]/1000); // Make the failesize into kilobytes & round it

$contentLengthKBITS=$filesize*8; // make kbytes into kbits
$bitrate=$bitratez/1000; //convert bits/sec to kbit/sec
$seconds=$contentLengthKBITS/$bitrate; // Calculate seconds in song

$playtime_mins = floor($seconds/60); // get the minutes of the playtime string
$playtime_secs = $seconds % 60; // get the seconds for the playtime string
if(strlen($playtime_secs)=='1'){$zero='0';} // if the string is a multiple of 10, we need to add a 0 for visual reasons
$playtime_secs = $zero.$playtime_secs; // add the zero if nessecary
$playtime_string=$playtime_mins.':'.$playtime_secs; // create the playtime string

echo $playtime_string;

?>

James Heinrich
getID3() v1 developer
Posts: 1471
Joined: Fri May 04, 2001 4:00 pm
Are you a spambot?: no
Location: Northern Ontario, Canada
Contact:

Re: External File Support

Post by James Heinrich » Mon Aug 01, 2011 7:26 pm

Referencing original post.

I'll see about making a demo file of how to do this kind of thing properly (possibly taking into account ID3v2 size), since (as you said) more than one person has asked about how to do something like this.

Note: remote file support will not be re-integrated into getID3 itself (it was removed in v1.5.6 since such code belongs outside the library).

mrid3rider
User
Posts: 2
Joined: Tue Jun 26, 2012 8:28 pm
Are you a spambot?: no

Re: External File Support

Post by mrid3rider » Tue Jun 26, 2012 8:35 pm

Dude this is a life-saver! Thanks for posting this up, saved me SO MUCH TIME! I assume it's okay to use this code on my own website as long as accreditation is maintained?

Post Reply