65.9K
CodeProject is changing. Read more.
Home

MP3FileInfo - Extract Header and ID3 Tags of an MP3 File

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.70/5 (30 votes)

May 31, 2003

2 min read

viewsIcon

253861

downloadIcon

13290

MP3FileInfo is able to extract both ID3v1 and ID3v2 Tags of an MP3 File.

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.

// 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