Click here to Skip to main content
15,882,113 members
Articles / Programming Languages / C++

A wrapper for the id3lib library

Rate me:
Please Sign up or sign in to vote.
4.75/5 (5 votes)
13 Sep 2013CPOL5 min read 33.5K   1.3K   22   9
This is a wrapper (in c++) for the id3lib library. It helps in getting and setting (reading and writing) id3 frames in media files that uses ID3 tagging.

Introduction

Encoding of PCM’s to format that is of smaller file size became very popular in the early 1990’s due to the growing desire to share audio files over the internet. Adding of track and album information to the files became also necessary and between mid-1990’s – the late 1990’s a group released what is known as ID3.

ID3 is a metadata (which contains information like artist, album, track length, album art, track number, genre, etc.) stored as chunks of data either at the beginning (ID3v2) or end (ID3v1) of the media file.

id3lib is an open source, cross-platform, software development library for reading, writing, and manipulating ID3v1 and ID3v2 tags.

This wrapper was written to ease working with id3lib.

Setting up your development environment

If you already know how to setup your development environment to link with id3lib, you can skip to the second part of this section.

Firstly, get the id3lib archive attached to this article (you can also get it from sourceforge.net/projects/id3lib/files/). The archive should contain a folder named “id3”, a header file named “id3.h”, a dll named “id3lib.dll”, an export library file named “id3lib.exp” and an Object File Library named “id3lib.lib”. Extract the archive.

  1. In Visual Studio, go to the Project menu then to Property Pages (<your project name> Property Pages). When the window opens, go to the Configuration Properties tab, then to the Linker section, under the Additional Library Directories, add the path of where you extract the archive to as the value
  2. Then go to the c/c++ tab and under Additional Include Directories, also add the extract path as it value
  3. Then go to the extract path and copy the id3lib.dll file to the Output directory of your project or to your system32 folder.

Your environment is ready for id3lib library. The id3lib.dll will have to be bundled will your executable.

Secondly, get the tagHelper_dd_mm_yyyy.zip archive attached to this article, extract it and copy the two files (tagHelper.h and tagHelper.cpp) to your project.

#include the header to your project

C++
#include "tagHelper.h" 

Using the wrapper

To use the wrapper, you will have to call the constructor with the filename of the media file you want to work on.

C++
#include "tagHelper.h" 
int main()
{
tagHelper th("c:/started.mp3");
return 0;
}

Reading Values

Various values can be read / gotten using the wrapper, this range from the file size, to the MPEGLayer of the mp3 file, to the channel information, to the title, artist, even to the albumart. ;-)

Get information (without using the getValue() method)

The following information about the media file can be gotten without the use of the getValue() method of the wrapper; hasLyrics(); hasV1Tag();hasV2Tag(); fileSize(); getMPEGLayer(); getMPEGVersion(); getMP3ChannelMode(); getCbrBitRate(); getVbrBitRate(); getFrequency(); getSampleRate(); getTrackLength(); and getMP3Header();

They are all members of the wrapper and their name suggests what they do

C++
tagHelper th("c:/started.mp3");
MP3_BitRates cbr;
cbr = th.getCbrBitRate();
printf("The cbr bitrate is: %i\n", cbr);

Mp3_ChannelMode channel;
channel = th.getMP3ChannelMode();
printf("The channel mode is: %i\n", channel);

uint32 length;
length = th.getTrackLength();
printf("The track length is: %isec\n", length);

Getting information with the getValue() method

The getValue() method of the wrapper can be used to get additional id3 tag information from the media file. The prototype of the getValue() method is

C++
char* tagHelper::getValue(ID3_FrameID); 

The method returns the value stored in the specified ID3_FrameID frame in character array. The ID3_FrameID are constants defined in the global.h header file that comes with the id3lib package. They are defined between lines 231 and 326 with description of what each stands for.

getValue() returns NULL if the ID3_FrameID specified is not present in the media file.

NOTE:

  1. Not all the ID3_FrameID are usable with getValue() because not all of them "store" their value as character array. Only use getValue() with those that accept character array.
  2. It is the responsibility of the coder to free (delete) the memory returned by getValue()
C++
tagHelper th("c:/started.mp3");
//Getting the genre information of the song
char* genre;
genre = th.getValue(ID3FID_CONTENTTYPE);
printf("The genre is: %s\n", genre);
delete genre; //always remember to delete this

//Getting the title of the song
char* title;
title = th.getValue(ID3FID_TITLE);
printf("The title of the song is: %s\n", title);
delete title; //always remember to delete this

Setting Information

The setValue() method can be used to create and or modify frames. The prototype is

C++
void tagHelper::setValue(ID3_FrameID, char*);

The method takes 2 parameters; the ID3_FrameID of the frame to update and the value to update it with. If the frame specified is not present in the media file, setValue() creates it and write the value to it.

NOTE:
  1. Not all the ID3_FrameID are usable with setValue() because not all of them "store" their value as character array. Only use setValue() with those that accept character array.
C++
tagHelper th("c:/started.mp3");
//setting the year of the song
th.setValue(ID3FID_YEAR, "1986");

//setting the track number of the song
th.setValue(ID3FID_TRACKNUM, "02");

The Albumart

The albumart ID3FID_PICTURE is one of such frame you can’t use getValue() and setValue() on because it content is not stored in character array (third time of saying that right?).

Adding albumart to your media file

The addAlbumart() method is used to add albumart to a media file.

C++
void tagHelper::addAlbumart(char*);

The method takes a parameter which is the path to the image file to set as the albumart. Note that if the path you specified does not exist, the frame is not updated. This means that if the media file contains an albumart before, it will not be removed.

C++
tagHelper th("c:/started.mp3");

//Adding an albumart
th.addAlbumart("c:/albumart.jpg");

Retrieving the albumart

The getAlbumart() method is used to extract the albumart of a media file into an image file. This is very useful in players that displays albumarts.

C++
bool tagHelper::getAlbumart(char*);

The method takes one parameter; where to save the extracted image. It returns true if successful and false if not.

C++
tagHelper th("c:/started.mp3");

//Extracting the albumart to c:/albumart_extracted.jpg
th.getAlbumart("c:/albumart_extracted.jpg");

Removing Tags (New)

Removing of tags might be necessary sometimes. This might range from removing unneeded information to removing personal information, or even to totally removing (stripping) all id3 tags.

removeTag()

The removeTag() method can be used to remove specific id3 tags from the media file.
C++
bool tagHelper::removeTag(ID3_FrameID);
This will return true if successful and false if the tag is not present or unsuccessful.
C++
tagHelper th("c:/started.mp3");

//Removing the title tag
th.removeTag(ID3FID_TITLE);

//Removing the albumart
th.removeTag(ID3FID_PICTURE);

removeAllTags()

This as it names imply removes all the id3 tags in the media file. BUT NOTE, it also removes the Mp3_Headerinfo so be careful about using it.

However, some portion (if not all) of the Mp3_Headerinfo seems to be recovered once the setValue() method is used on a media file which removeAllTags() had been used on before

This is useful in the case of decoding an mp3 file into a wav file. It solves the problem of having to seek back and forth so as to remove the id3 portion of the file before trying to decode it, just removeAllTags() the file and decode!

C++
tagHelper th("c:/started.mp3");

//Stripping all id3 tags
th.removeAllTags();

History

13th of September, 2013
  1. getFrequency(); removed as it same as getSampleRate();
  2. Tag Removing added (bool removeTag(ID3_FrameID);)
  3. Stripping of all id3 tags added (bool removeAllTags();)
31st August, 2013 - Initial Release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Nigeria Nigeria
A graduate of Agricultural Engineering from Ladoke Akintola University of Technology, Ogbomoso but computer and web programming is his first love. You can meet him on Facebook Osofem Inc.

Comments and Discussions

 
PraiseA great little class. Works a treat Pin
Member 1540518016-May-23 5:04
Member 1540518016-May-23 5:04 
Questionvb.net??? Pin
Member 129725221-Feb-20 13:26
Member 129725221-Feb-20 13:26 
Questiontext format -- does it work with accented characters??? Pin
Member 129725221-Feb-20 13:25
Member 129725221-Feb-20 13:25 
QuestionThank you, thank you, thank you! Pin
Member 1129572523-Feb-16 1:36
Member 1129572523-Feb-16 1:36 
QuestionDLL file error Pin
Member 115846747-Apr-15 4:24
Member 115846747-Apr-15 4:24 
QuestionOne file is missing Pin
e.duardo24-Nov-14 5:56
e.duardo24-Nov-14 5:56 
AnswerRe: One file is missing Pin
Oso Oluwafemi Ebenezer24-Nov-14 20:21
Oso Oluwafemi Ebenezer24-Nov-14 20:21 
GeneralMy vote of 4 Pin
The_Inventor31-Aug-13 22:37
The_Inventor31-Aug-13 22:37 
Concerning recent questions about id3lib, and albumart, it is the answer to the question, from the proper dissection of the library. Reasonably clear in its writing.
GeneralRe: My vote of 4 Pin
Oso Oluwafemi Ebenezer3-Sep-13 0:20
Oso Oluwafemi Ebenezer3-Sep-13 0:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.