Wednesday, September 9, 2009

PHP class mp3Data – Get ID3 Info from MP3

So recently, I submitted a bid on a project to make a lyrics website using
PHP.  I haven’t been contacted about whether or not I won the bid yet, but I decided that I might
as well start some preliminary work on backend coding – It’ll make my life easier, and deliver a faster and
better result to the client, so why not?

Why am I mentioning this, you ask? 
What does my personal life have to do with my tech blog?  Well, I’m getting to that!

I
almost immediately thought.  “Hey, wouldn’t it be really cool if the admin of the website could
simply upload an MP3 file and pull almost, if not all of the data from it to use on the site?” After which,
I quickly searched around for something that would allow me to do just that, to read the ID3 tags on an MP3
file.

The first thing I came to, was the search result on the PHP website for “id3,” the
ID3 PECL Library
Unfortunately, after trying for a very long time on DreamHost, I not only couldn’t get it to install, but
had just wasted probably an hour in an attempt to.  Not only that, but the library was still in its
alpha stage.

Some quick internet searching took me to the href="http://getid3.sourceforge.net/">getid3 library, a robust library programmed to read ID3
data tags (and other types of metadata?) from almost any type of file.  I quickly set about to
using it, and found its internal structure complicated and confusing.  It didn’t even offer me a
quick and easy way to access the album artwork.

After a good period of time learning some
of the basic structure of ID3 and how the getid3 library worked, I set about to coding up a library of my
own to quickly interface with getid3 and retrieve the information I needed.

The result is
class::mp3Data.  A very, very simple library
built exclusively to interface with getid3 and retrieve basic information about an mp3 file while still
retaining more advanced information for coders who need it.  Take a look at some example code and
the licensing permissions after the break.

Creative Commons License src="http://i.creativecommons.org/l/by-nc-sa/3.0/us/80x15.png" />


Retrieving and Displaying the Artwork as a Picture:

style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom:
4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%;
padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr;
max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right:
silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
 1:
<?php



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 2: require_once("mp3Data.class.php");



 3: $data =             style="color: #0000ff">new mp3Data("filename.mp3");




 4: $img = $data->getArt();




 5: if ($img)



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 6: {



 7: header(            style="color: #006080">"Content-type: image/jpeg");




 8: imagejpeg($img);




 9: }




 10: ?>




Displaying
Basic Data:







 1: <?php




 2: require_once("mp3Data.class.php");




 3: $data =             style="color: #0000ff">new mp3Data("filename.mp3");




 4: print("Title: ".$data->getName().            style="color: #006080">"<br />");




 5: print("Album: ".$data->getAlbum().            style="color: #006080">"<br />");




 6: print("Artist: ".$data->getArtist());




 7: ?>




You
can copy the source code from pastebin at this
link
.



Licensing Permissions:



href="http://creativecommons.org/licenses/by-nc-sa/3.0/us/" rel="license"> style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px"
alt="Creative Commons License" src="http://i.creativecommons.org/l/by-nc-sa/3.0/us/80x15.png" />



style="display: none">



property="dc:title" href="http://purl.org/dc/dcmitype/Text" xmlns:dc="http://purl.org/dc/elements/1.1/">class::mp3Data
by property="cc:attributionName" xmlns:cc="http://creativecommons.org/ns#">Navarr Barnier is
licensed under a Creative
Commons Attribution-Noncommercial-Share Alike 3.0 United States License
.


Based on a work at pastebin.ca.




Permissions beyond the scope of this license may be available at href="mailto:[email protected]" rel="cc:morePermissions" xmlns:cc="http://creativecommons.org/ns#">mailto:[email protected].


6 comments:

  1. [...] Code is located at pastebin and retains the same usage permissions as v0.1.
    [...]

    ReplyDelete
  2. I found this very helpful, thank you!

    ReplyDelete
  3. Hi! Very useful, thanks for your colla bro.

    Im using it and works
    perfectly, thanks bro. But, can you create a set of setters aside from the getters?


    Hope
    you can :D

    ReplyDelete
  4. I'm having some problems displaying the image, can you help me? Is that jpeg image
    format displaying way for every id3Tags for each mp3?

    ReplyDelete
  5. We have sell some products of different custom boxes.it is very useful and very low price please visits this site thanks and please share this post with your friends. tubidy mp3

    ReplyDelete
  6. By the age of 11, there appeared to be little that any music educator could show George; it was now that George's dad started furious and again communicated his longing for George to stop playing in the music, and to come all the way back and do as he wished. https://many-ringtones.com/13245-michigan-marching-band-lets-go-blue.html Michigan Marching Band Lets Go Blue Ringtone

    ReplyDelete