Getting Meta Data out of mp3 files

MPEG-1 or MPEG-2 Audio Layer III, more commonly referred to as MP3, is an audio coding format for digital audio which uses a form of lossy data compression. Having some mp3 files on your server and want to get details about them? Yes? Okay, so let's do this with PHP.

First of all, you'll need to go to getID3() and get ID3. The following code works with both ID3 v1 and v2. Reads more than just ID3 but should fit the bill.

<?php
class CMP3File { 
 var $title;var $artist;var $album;var $year;var $comment;var $genre; 
 function getid3 ($file) { 
  if (file_exists($file)) { 
   $id_start=filesize($file)-128; 
   $fp=fopen($file,"r"); 
   fseek($fp,$id_start); 
   $tag=fread($fp,3); 
   if ($tag == "TAG") { 
    $this->title=fread($fp,30); 
    $this->artist=fread($fp,30); 
    $this->album=fread($fp,30); 
    $this->year=fread($fp,4); 
    $this->comment=fread($fp,30); 
    $this->genre=fread($fp,1); 
    fclose($fp); 
    return true; 
   } else { 
    fclose($fp); 
    return false; 
   } 
  } else { return false; } 
 } 
} 
?>

Then save this with a name, say, CMP3File.php. Now, the following code displays the details of the mp3 file, like, the name, artist, album, year, etc. using the class 'CMP3File'.

<?php 
include ("CMP3File.php"); 
$filename="music_file.mp3"; 
$mp3file=new CMP3File; 
$mp3file->getid3($filename); 
echo "Title: $mp3file->title<br>\n"; 
echo "Artist: $mp3file->artist<br>\n"; 
echo "Album: $mp3file->album<br>\n"; 
echo "Year: $mp3file->year<br>\n"; 
echo "Comment: $mp3file->comment<br>\n"; 
echo "Genre: " . Ord($mp3file->genre) . "<br>\n";
?>
Emoticon? Emoticon

Komentar Terbaru

Just load it!