CodeProject: MP3FileInfo - Extract Header and ID3 Tags of an MP3 File. Free source code and programming help

本文介绍了一个用于读取MP3文件元数据的C++类库MP3FileInfo,该库作为id3lib的一个封装,提供了简化接口来获取MP3文件的详细信息,包括文件大小、比特率、采样率等技术参数,以及艺术家、专辑、歌曲标题等ID3标签内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导读:


















28 votes for this Article.







Popularity: 6.42 Rating: 4.44 out of 5










1

2

3

4

5




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



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


About the Author






whizer












Occupation: Web Developer
Location: United Kingdom






Other popular Audio and Video articles:




本文转自
http://www.codeproject.com/KB/audio-video/mp3fileinfo.aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值