Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   MP3 renamer in PHP (http://www.go4expert.com/articles/mp3-renamer-php-t805/)

pradeep 17May2006 00:01

MP3 renamer in PHP
 
Well, I always had a problem identifying my MP3 files because of their vague filenames like, "Track 1.mp3" or "Bryan Adams 1.mp3", I was wondering whether I could rename the file depending upon the MP3 tags, so I came with up this script with reads MP3 tags and renames the file according to the format you specify.

To run the script you only need to have any version php installed, you can run it from a web server or from the command line.

I wish to improver the script in future, so that you can categorise your music in whtever way you want, support for more file formats, and even batch edit your MP3 tags. For the time being try out this script.

Code: php

<?
  $path = "g:/english/bryan adams/"; // path where the mp3s are located
 
  //make a array of genres
  $genre_arr = array("Blues","Classic Rock","Country","Dance","Disco","Funk","Grunge",
   "Hip-Hop","Jazz","Metal","New Age","Oldies","Other","Pop","R&B",
   "Rap","Reggae","Rock","Techno","Industrial","Alternative","Ska",
   "Death Metal","Pranks","Soundtrack","Euro-Techno","Ambient",
   "Trip-Hop","Vocal","Jazz+Funk","Fusion","Trance","Classical",
   "Instrumental","Acid","House","Game","Sound Clip","Gospel",
   "Noise","AlternRock","Bass","Soul","Punk","Space","Meditative",
   "Instrumental Pop","Instrumental Rock","Ethnic","Gothic",
   "Darkwave","Techno-Industrial","Electronic","Pop-Folk",
   "Eurodance","Dream","Southern Rock","Comedy","Cult","Gangsta",
   "Top 40","Christian Rap","Pop/Funk","Jungle","Native American",
   "Cabaret","New Wave","Psychadelic","Rave","Showtunes","Trailer",
   "Lo-Fi","Tribal","Acid Punk","Acid Jazz","Polka","Retro",
   "Musical","Rock & Roll","Hard Rock","Folk","Folk-Rock",
   "National Folk","Swing","Fast Fusion","Bebob","Latin","Revival",
   "Celtic","Bluegrass","Avantgarde","Gothic Rock","Progressive Rock",
   "Psychedelic Rock","Symphonic Rock","Slow Rock","Big Band",
   "Chorus","Easy Listening","Acoustic","Humour","Speech","Chanson",
   "Opera","Chamber Music","Sonata","Symphony","Booty Bass","Primus",
   "Porn Groove","Satire","Slow Jam","Club","Tango","Samba",
   "Folklore","Ballad","Power Ballad","Rhythmic Soul","Freestyle",
   "Duet","Punk Rock","Drum Solo","Acapella","Euro-House","Dance Hall");
 
  /*
      rename formats:
      %AR    - Artist
      %SN    - Song Name
      %G    - Genere
      %AB    - Album
      %YR    - Year
 
      The deafult format is "%AR - %SN" i.e. "Artist - Song Name"
  */

 
 
  function rename_mp3_from_tag($start_path,$format = "%AR - %SN")
  {
      $h = opendir($start_path) or die("Couldn't open directory");
      while(false !== ($file=readdir($h)))
      {
          if($file != '.' && $file != '..')
          {
              if(is_file("$start_path/$file") && preg_match("/\.mp3$/i",$file))
              {
                  $info = read_tag("$start_path/$file");
                  if(isset($info['song']) && isset($info['artist']))
                  {
                      $matches = array("/%AR/","/%SN/","/%G/","/%AB/","/%YR/");
                      $replaces = array($info['artist'],$info['song'],$info['genre'],$info['album'],$info['year']);
                      $f = preg_replace($matches,$replaces,$format);
                      $f .= ".mp3";
                      if(rename("$start_path/$file","$start_path/$f"))
                          print "Rename: $file -> $f\n";
                      else
                          print "Failed for $file\n";
                  }
              }
              else if(is_dir("$start_path/$file"))
              {
                  rename_mp3_from_tag("$start_path/$file"); /* recursive to recurse directories */
              }
          }
      }
      closedir($h);
  }
 
  rename_mp3_from_tag($path);
 
  function read_tag($mp3_f)
  {
       global $genre_arr;
       $file = fopen($mp3_f, "r") or die("Could not open mp3_f");
       fseek($file, -128, SEEK_END);
       
       $tag = fread($file, 3);
       
       if($tag == "TAG")
       {
           $data["song"] = trim(fread($file, 30));
           $data["artist"] = trim(fread($file, 30));
           $data["album"] = trim(fread($file, 30));
           $data["year"] = trim(fread($file, 4));
           $data["comment"] = trim(fread($file, 30));
           $data["genre"] = $genre_arr[ord(trim(fread($file, 1)))];
           
       }
       else
       {
           print("MP3 file does not have any ID3 tag!\n");
           fclose($file);
           return array();
       }
 
       fclose($file);
       return $data;
  }
 
  ?>


Sample Output:
Code:

  Rename: song (6).mp3 -> Bryan Adams - Depend On Me.mp3
  Rename: song (7).mp3 -> Bryan Adams - Do i Have To Say The Words.mp3
  Rename: song (2).mp3 -> Bryan Adams - Back To You.mp3
  Rename: song (1).mp3 -> Bryan Adams - Dont Drop That Bomb On Me.mp3
  Rename: song (5).mp3 -> Bryan Adams - Clound No 9.mp3
  Rename: song (8).mp3 -> Bryan Adams - Don't Give Up.mp3
  Rename: song (3).mp3 -> Bryan Adams - Best Of Me.mp3
  Rename: song (4).mp3 -> Bryan Adams - Can't Stop This thing.mp3
  Rename: song (9).mp3 -> Bryan Adams - Don't Leave Me Lonely.mp3
  Rename: song (10).mp3 -> Bryan Adams - Everything I Do.mp3
  Rename: song (11).mp3 -> Bryan Adams - Fits Ya Good (Unplugged).mp3
  Rename: song (12).mp3 -> Bryan Adams - Have You Ever Really Loved.mp3
  Rename: song (13).mp3 -> Bryan Adams - Heat Of The Night.mp3
  Rename: song (14).mp3 -> Bryan Adams - I'm Ready.mp3
  Rename: song.mp3 -> Bryan Adams - Inside Out.mp3



All times are GMT +5.5. The time now is 11:21.