Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / MFC
Article

MP3FileInfo - Extract Header and ID3 Tags of an MP3 File

Rate me:
Please Sign up or sign in to vote.
4.70/5 (32 votes)
30 May 20032 min read 250.9K   13.3K   119   36
MP3FileInfo is able to extract both ID3v1 and ID3v2 Tags of an MP3 File.

Image 1

Introduction

In the need of structuring my MP3 collection, I wrote the class MP3FileInfo as a read-only wrapper for id3lib. id3lib is an open-source software library for manipulating ID3v1/v1.1 and ID3v2 tags. Since id3lib is a very powerful library, the API is also quite complex. That's why a wrapper class with a more interface is very useful.

How to use

MP3FileInfo uses a slightly different initialization concept. The constructor does nothing apart from initializing all member variables to zero/false. A call to Init() is needed in order to extract the actual data. But one can call Init() as often as wanted without any memory leak. All data is being freed before the data of the new file is extracted.

C++
// create object (don't use it at this point)
MP3FileInfo obj;

// Initialize object with data
if(!obj.Init("some-file.mp3"))
 {
  // handle error ...
  // don't use object (except Init()) after this point
 }

// ... use MP3FileInfo ...

It is recommended to check MP3FileInfo::isOK() before doing any action.

Interface

After successfully calling Init(const char* cszFilename), the class provides the following public members:

File-specific

  • char* szFilename - Zero terminated Filename as supplied by Init(...)
  • int nFileSize - Size of the file in bytes
  • bool bHasV1Tag - Whether the file contains an ID3v1 tag
  • bool bHasV2Tag - Whether the file contains an ID3v2 tag
  • bool bHasLyrics - Whether the file contains Lyrics

Header-specific:

  • int nLength - Length of song in seconds
  • int nCbrBitRate - Bitrate in bits per second (is 0 if file uses variable bitrate)
  • int nVbrBitRate - Average bitrate in bits per second (is 0 if file uses constant bitrate)
  • int nBitRate - Bitrate in bits per second (if file uses constant bitrate, nCbrBitRate is used otherwise nVbrBitRate)
  • int nSampleRate - Sample rate of file in Hertz
  • char* szBitRate - Human readable string containing bitrate information, e.g. "160 kbit/s" or "147 kbit/s (VBR)"
  • char* szMpegLayer - MPEG layer information, e.g. "Layer III"
  • char* szMpegVersion - MPEG version information, e.g. "MPEG 1"
  • char* szChannelMode - Channel mode, e.g. "Stereo", "Single Channel"

ID3v1 Tag Elements (and up):

  • char* szArtist; - Artist of song
  • char* szTitle; - Song title
  • char* szAlbum; - Album
  • char* szComment; - Comment
  • char* szTrack; - Track description (may contain non-numeric characters)
  • char* szYear; - Year of publication/performance (may contain non-numeric characters)
  • char* szGenre; - Name of Genre
  • int nTrack; - Track number extracted of szTrack
  • int nYear; - Year of publication/performance extract of szYear

ID3v2-specific Tag Elements (only common):

  • char* szComposer; - Composer of song
  • char* szCopyright; - Copyright notices
  • char* szEncodedBy; - Encoded by, often used by audio converting programs like Exact Audio Copy
  • char* szOriginalArtist; - Original Artist, useful if the song is covered
  • char* szURL; - URL, e.g. Link to the homepage of an artist

Note: Most strings and numbers may be zero if a field isn't defined. The ID3v2-specific strings are only available if bHasV2Tag is true (should be obvious). All ID3v1-specific elements are superceded by the corresponding ID3v2 field if present and non-zero/non-empty.

History

  • May 31, 2003 - First Release 1.0.0

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: Got the DLL? Pin
whizer7-May-05 9:00
whizer7-May-05 9:00 
QuestionCompatibility with C++ Builder? Pin
Member 16357601-Feb-05 8:08
Member 16357601-Feb-05 8:08 
AnswerRe: Compatibility with C++ Builder? Pin
whizer1-Feb-05 8:39
whizer1-Feb-05 8:39 
GeneralDon't use sprintf to copy strings Pin
FigBug22-Jun-04 19:20
FigBug22-Jun-04 19:20 
GeneralInsert message digest in id3 tag Pin
ee_tkwaa5-Feb-04 15:16
ee_tkwaa5-Feb-04 15:16 
GeneralI have problem to download the files Pin
Anonymous18-Nov-03 8:54
Anonymous18-Nov-03 8:54 
GeneralRe: I have problem to download the files Pin
Jan R Hansen22-Dec-03 22:21
Jan R Hansen22-Dec-03 22:21 
GeneralAudio Pin
Sarat Babu12-Oct-03 19:32
Sarat Babu12-Oct-03 19:32 
Hi all.

I had a problem to find Beats per minute in WAVE, MP3, WMA etc. Can anyone pls help me out with sample code or algorithms, or in other manner. This is a very very urgent assignment.


Thanks you all in advance

K Sarat Babu
GeneralRe: Audio Pin
whizer12-Oct-03 21:52
whizer12-Oct-03 21:52 
GeneralLength of the file Pin
Bob Stanneveld24-Aug-03 13:23
Bob Stanneveld24-Aug-03 13:23 
GeneralOgg Support Pin
Testosteles9-Jun-03 5:25
Testosteles9-Jun-03 5:25 
GeneralRe: Ogg Support Pin
whizer10-Jun-03 0:02
whizer10-Jun-03 0:02 
GeneralSuggestions Pin
Uwe Keim1-Jun-03 2:41
sitebuilderUwe Keim1-Jun-03 2:41 

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.