matt hackmann -> web developer

ID3Lib - A tag library for all seasons

Posted on in programming php id3lib c++ .net

image

ID3Lib [ Download ]

As one may have derived from previous posts, I have a project the uses MP3s quite heavily. As such, retrieving tag data from these is quite an important thing. Having recently migrated my personal server to Windows, I needed a non-Linux variant of my previous tag read. I thought that while I was at it I could add an additional feature, namely saving embedded album art. Unfortunately, I was unable to find any clear directions on how to do so using TagLib, so I created my own library. Three different times. In three different languages. A C++ version, a .NET version and, finally, a PHP version.

Each of these libraries works pretty similar, but to give you the general idea, I'll write out a simple program for each.

Code: c++
  1. #include <iostream>
  2. #include "id3lib.h"
  3. using namespace std;
  4. void main () {
  5. ID3Lib id3("my.mp3"); // Create the object
  6. if (!id3)
  7. cout << "Unable to parse file";
  8. else {
  9. cout << "Title: " << id3.artist() << "n"; // Output title using shortcut method
  10. cout << "Album: " << id3.tag("TALB") << "n"; // Can retrieve any tag with this method
  11. id3.saveAlbumArt ("album_art.jpg"); // Save any embedded picture
  12. }
  13. }
Code: c#
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Id3Lib_Net;
  5. namespace Id3LibNet_Test
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. Reader t = new Reader("my.mp3"); // Load the MP3 file
  12. Console.WriteLine(t.title()); // Print out the title using shorthand method
  13. Frame album = t.getFrame("TALB"); // All loaded tags can be retrieved this way
  14. t.savePicture ("album_art.jpg");
  15. }
  16. }
  17. }
Code: php
  1. <?php
  2. include ("id3lib.php");
  3. $id3 = new ID3Lib ("my.mp3"); // Create the ID3Lib object
  4. if (!$id3->getErr()) { // Will be null if load was successful
  5. echo ("Title: ".$id3->title); // A shortcut for getting the title
  6. echo ("Album: ".$id3->tags->TALB); // All tags can be accessed this way
  7. $id3->savePicture ("album_art.jpg"); // Saves an embedded picture
  8. }
  9. else
  10. echo ("There was an error opening the MP3: ".$id3->getErr());
  11. ?>

Each library includes a set of methods/properties for the following common tags: title, album, artist, disc, track, year.

All three libraries are licensed under the GPLv3 and have the original source code, so you can do with them what you wish, though credit and a link back here is always nice :-).

ID3Lib [ Download ]

Disclaimer: I have only tested these on MP3s I have available to me, so it's very possible that you may have issues with this library. If you do, please leave a comment and I'll update the libs accordingly.

Comments

Posted on by linkman2004 (linkman2004)
linkman2004

Wazzup, bitch?

Add Comment

Sign-in with Twitter

To prevent the propagation of evil, evil spam, we require that you link your Twitter account with us. Don't worry, it's all legit and we're not storing any passwords or anything. Just to be safe, though, be sure to check the address bar after you click the "Sign in with Twitter" button and make sure that the address begins with https://twitter.com. If it doesn't, please contact me ASAP.