Click here to Skip to main content
16,017,231 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 253.7K   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

 
Questionmove to qt creator project ? Pin
atx2376-Jun-14 3:21
atx2376-Jun-14 3:21 
GeneralMy vote of 5 Pin
james_chyn10-Oct-11 17:53
james_chyn10-Oct-11 17:53 
GeneralMy vote of 5 Pin
paul116728-Sep-11 21:54
paul116728-Sep-11 21:54 
Generalbug,occur one bug. Pin
zhang_19855-Aug-09 22:08
zhang_19855-Aug-09 22:08 
Generalincorrect duration and bitrate Pin
autotesting11-Jan-09 10:17
autotesting11-Jan-09 10:17 
Generalusing to eVC++ Pin
kimhoontae15-Nov-07 21:01
kimhoontae15-Nov-07 21:01 
GeneralRe: using to eVC++ Pin
whizer16-Nov-07 0:58
whizer16-Nov-07 0:58 
GeneralClumsy program Pin
Drexel Gal28-Jul-07 18:44
Drexel Gal28-Jul-07 18:44 
GeneralAnother linkerror Pin
sonocln14-Jun-07 10:54
sonocln14-Jun-07 10:54 
GeneralRe: Another linkerror Pin
whizer14-Jun-07 11:38
whizer14-Jun-07 11:38 
GeneralForeign languaga Pin
HYCKen27-Jan-07 4:23
HYCKen27-Jan-07 4:23 
AnswerRe: Foreign languaga Pin
whizer27-Jan-07 5:12
whizer27-Jan-07 5:12 
Generalstatic lib...how? [modified] Pin
aullidolunar11-Nov-06 14:47
aullidolunar11-Nov-06 14:47 
GeneralRe: static lib...how? Pin
whizer12-Nov-06 11:20
whizer12-Nov-06 11:20 
GeneralError Compiling Pin
Daniel Ahdoot16-Aug-06 11:53
Daniel Ahdoot16-Aug-06 11:53 
AnswerRe: Error Compiling [modified] Pin
whizer16-Aug-06 22:47
whizer16-Aug-06 22:47 
Generalerror szComment Pin
tenstars26-Feb-06 21:09
tenstars26-Feb-06 21:09 
GeneralLinkers error Pin
rayman228-Dec-05 11:39
rayman228-Dec-05 11:39 
GeneralRe: Linkers error Pin
whizer28-Dec-05 23:09
whizer28-Dec-05 23:09 
GeneralRe: Linkers error Pin
rayman229-Dec-05 1:25
rayman229-Dec-05 1:25 
GeneralRe: Linkers error Pin
WhiteWolf[SLO]29-Dec-05 14:00
WhiteWolf[SLO]29-Dec-05 14:00 
GeneralBUG FIX! Pin
win32_coder7-Sep-05 1:43
win32_coder7-Sep-05 1:43 
QuestionGot the DLL? Pin
roopeshsheth7-May-05 7:19
roopeshsheth7-May-05 7:19 
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 

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.