Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read remote mp3 file information using php

I am working on a site which will fetch mp3 details from a remote url. I need to write a cron job so that it gets all the song information such as the file name, path, artist, genre, bitrate, playing time, etc and put it in a database table.

I tried getid3 package, but this is very slow when fetching more than one url at a time and I get maximum execution error.

Example:

require_once('getid/getid3/getid3.php');

    $urls = array ('http://stackoverflow.com/test1.mp3','http://stackoverflow.com/test2.mp3''http://stackoverflow.com/test3.mp3');


    foreach($urls  as $ur){
      $mp3_det = getMp3Info( $ur );
      print_r ($mp3_det);

    }

    function getMp3Info ( $url ){
            if($url){
            /**********/
            $filename = tempnam('/tmp','getid3');
            if (file_put_contents($filename, file_get_contents($url, false, null, 0, 35000))) {
               if (require_once('getid/getid3/getid3.php')) {
                  $getID3 = new getID3;
                  $ThisFileInfo = $getID3->analyze($filename);
                  unlink($filename);
                  $bitratez = $ThisFileInfo[audio][bitrate] ? $ThisFileInfo[audio][bitrate] : '';
                  $headers = get_headers($url, 1);
                  if ((!array_key_exists("Content-Length", $headers))) { return false; }
                 // print $headers["Content-Length"];
                  $filesize= round($headers["Content-Length"]/1000);
                  $contentLengthKBITS=$filesize*8;
                  if ( $bitratez ){
                         $bitrate= round ( $bitratez/1000 );
                        $seconds=$contentLengthKBITS/$bitrate;
                        $playtime_mins = floor($seconds/60);
                        $playtime_secs = $seconds % 60;

                         if(strlen($playtime_secs)=='1'){$zero='0';}
                        $playtime_secs = $zero.$playtime_secs;
                        $playtime_string=$playtime_mins.':'.$playtime_secs;
                    }
                    else $playtime_string='0:00';

                 // echo '<pre>'.print_r($ThisFileInfo, true).'</pre>';
               }

               $bitrate = $bitrate ? $bitrate : 0;

                $ret = array();
                $ret['playtime'] = $playtime_string;
                $ret['filesize'] = $filesize;
                $ret['bitrate']  = $bitrate;

               return $ret;
            }
        }
like image 252
Renjith R Avatar asked Feb 23 '26 14:02

Renjith R


1 Answers

You may be able to help the execution time by using a socket connection and reading in chunks of the file at a time, and continuously trying to analyze the file.

Since ID3 data is stored in the beginning of the mp3, there is no point in downloading the entire thing. THe biggest problem I see right now is that the analyze function only takes a filename, not binary data (which is what you would have). So, you would have to either update the code, or make a similar function to analyze that works with your binary data.

like image 124
sberry Avatar answered Feb 25 '26 02:02

sberry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!