MP3FileInfo - Extract Header and ID3 Tags of an MP3 File






4.70/5 (30 votes)
May 31, 2003
2 min read

253861

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 byInit(...)
int nFileSize
- Size of the file in bytesbool bHasV1Tag
- Whether the file contains an ID3v1 tagbool bHasV2Tag
- Whether the file contains an ID3v2 tagbool bHasLyrics
- Whether the file contains Lyrics
Header-specific:
int nLength
- Length of song in secondsint 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 otherwisenVbrBitRate
)int nSampleRate
- Sample rate of file in Hertzchar* 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 songchar* szTitle;
- Song titlechar* szAlbum;
- Albumchar* szComment;
- Commentchar* szTrack;
- Track description (may contain non-numeric characters)char* szYear;
- Year of publication/performance (may contain non-numeric characters)char* szGenre;
- Name of Genreint nTrack;
- Track number extracted of szTrackint nYear;
- Year of publication/performance extract of szYear
ID3v2-specific Tag Elements (only common):
char* szComposer;
- Composer of songchar* szCopyright;
- Copyright noticeschar* szEncodedBy;
- Encoded by, often used by audio converting programs like Exact Audio Copychar* szOriginalArtist;
- Original Artist, useful if the song is coveredchar* 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