Click here to Skip to main content
15,888,521 members
Articles / Programming Languages / Visual Basic
Article

Read MP3 header information and read/write the ID3v1 tag

Rate me:
Please Sign up or sign in to vote.
4.35/5 (32 votes)
1 Mar 20051 min read 273.4K   3.9K   83   44
A small solution for reading MPEG audio headers and the ID3v1 tag

Introduction

There are many other articles at The Code Project describing the audio header and tag information, but none in VB.NET. So it's time to update my very old article (Reading and writing MP3 ID3v1 tags ) with nicer code and extend it to read the MPEG header information.

I don't want to go into detail about the structure of MPEG audio headers, because Konrad Windszus already has a very good article (MPEG Audio Frame Header) about that. If you're interested in the structure of the audio header or the ID3 tag format i recommend to visit the following sites:

Using the code

I wrote two classes to handle the header and tag information, called MP3Info and ID3v1Tag. The following code will show the structure to use the classes:

VB
Dim objMP3Info As New _
 Monotic.Multimedia.MP3.MP3Info
With ListView1
    ''' Set the filename property
    objMP3Info.Filename = "c:\test.mp3"

    ''' Add the header information to a listview
    .Add("Filesize").SubItems.Add(objMP3Info.Filesize & " Byte")
    .Add("SamplingRateFrequency").SubItems.Add _
     (objMP3Info.SamplingRateFrequency & " Hz")
    .Add("Padding").SubItems.Add(objMP3Info.Padding & " Bytes")
    .Add("Private").SubItems.Add(objMP3Info.PrivateBit)
    .Add("Copyright").SubItems.Add(objMP3Info.Copyright)
    .Add("OriginalBit").SubItems.Add(objMP3Info.OriginalBit)
    .Add("Bitrate").SubItems.Add(objMP3Info.Bitrate & " bps")
    .Add("FrameSamples").SubItems.Add(objMP3Info.FrameSamples)
    .Add("FrameSize").SubItems.Add(objMP3Info.FrameSize & " Byte")
    .Add("Length").SubItems.Add(objMP3Info.Length & " s (" 
     & Int(objMP3Info.Length / 60) & ":" & _
      objMP3Info.Length Mod 60 & " m)")
    .Add("HeaderPosition").SubItems.Add(objMP3Info.HeaderPosition)
    .Add("VBRScale").SubItems.Add(objMP3Info.VBRScale)

    Select Case objMP3Info.MPEGVersion
        Case MP3.MPEGVersionEnum.MPEG1
            .Add("MPEGType").SubItems.Add("MPEG 1")
        Case MP3.MPEGVersionEnum.MPEG2
            .Add("MPEGType").SubItems.Add("MPEG 2")
        Case MP3.MPEGVersionEnum.MPEG25
            .Add("MPEGType").SubItems.Add("MPEG 2.5")
    End Select

    Select Case objMP3Info.Layer
        Case MP3.LayerEnum.LayerI
            .Add("Layer").SubItems.Add("Layer I")
        Case MP3.LayerEnum.LayerII
            .Add("Layer").SubItems.Add("Layer II")
        Case MP3.LayerEnum.LayerIII
            .Add("Layer").SubItems.Add("Layer III")
    End Select

    Select Case objMP3Info.Protection
        Case MP3.ProtectionEnum.None
            .Add("Protection").SubItems.Add("None")
        Case MP3.ProtectionEnum.CRC
            .Add("Protection").SubItems.Add("By CRC")
    End Select

    Select Case objMP3Info.ChannelMode
        Case MP3.ChannelModeEnum.DualChannel
            .Add("ChannelMode").SubItems.Add("Dual Channel")
        Case MP3.ChannelModeEnum.JointStereo
            .Add("ChannelMode").SubItems.Add("Joint Stereo")
        Case MP3.ChannelModeEnum.SingleChannel
            .Add("ChannelMode").SubItems.Add("Single Channel")
        Case MP3.ChannelModeEnum.Stereo
            .Add("ChannelMode").SubItems.Add("Stereo")
    End Select

    Select Case objMP3Info.Emphasis
        Case MP3.EmphasisEnum.CCIT
            .Add("Emphasis").SubItems.Add("CCIT")
        Case MP3.EmphasisEnum.MS5015
            .Add("Emphasis").SubItems.Add("50/15 ms")
        Case MP3.EmphasisEnum.None
            .Add("Emphasis").SubItems.Add("None")
    End Select

    Select Case objMP3Info.Encoding
        Case MP3.EncodingEnum.CBR
            .Add("Encoding").SubItems.Add("CBR")
        Case MP3.EncodingEnum.VBR
            .Add("Encoding").SubItems.Add("VBR")
    End Select

    ''' Add the ID3v1 tag information to a listview
    If (objMP3Info.ID3v1Tag.TagAvailable) Then
        .Add("ID3 Title").SubItems.Add _
         (objMP3Info.ID3v1Tag.Title)
        [...]
    End If

    ''' Update the tag
    objMP3Info.ID3v1Tag.Title = "Another title"
    objMP3Info.ID3v1Tag.Update()

End With

Please have a look at the sample and the class code to see all the features.

Points of Interest

Unlike other articles, this class will handle CBR and VBR encoded files, so the playtime is calculated correctly.

The class is well-commented with XML comments made by AxTools CodeSmart 2005 and NDoc . I included the generated HTML help file in the download.

I'm working on reading and writing the much more complex ID3v2.x tags at the moment (80% done, just compressed frames are a little bit tricky). If you are interested in this, i will update this article in the future.

History

27.02.2004 Release of version 1

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
Software Developer (Senior) CIBER AG
Germany Germany
Feel free to contact me via Email or MSN Messenger (thommy@live.com).

Comments and Discussions

 
QuestionRe: Update Missing Pin
mazin221-Dec-10 13:08
mazin221-Dec-10 13:08 
AnswerRe: Update Missing Pin
Esquilax19793-Nov-11 8:29
Esquilax19793-Nov-11 8:29 
QuestionPassword??? Pin
Godz1lla1-Jan-06 12:44
Godz1lla1-Jan-06 12:44 
AnswerRe: Password??? Pin
Thommy Mewes17-Jan-06 8:45
Thommy Mewes17-Jan-06 8:45 
GeneralException by reading header info from CD Pin
Ronny Foerster29-May-05 20:03
Ronny Foerster29-May-05 20:03 
GeneralRe: Exception by reading header info from CD Pin
Thommy Mewes16-Jun-05 12:21
Thommy Mewes16-Jun-05 12:21 
GeneralRe: Exception by reading header info from CD Pin
Jef Lagueux21-Mar-06 12:59
Jef Lagueux21-Mar-06 12:59 
QuestionRe: Exception by reading header info from CD Pin
KaosKidd7-Dec-06 4:11
KaosKidd7-Dec-06 4:11 
GeneralRe: Exception by reading header info from CD Pin
cicijay5-Aug-07 9:32
cicijay5-Aug-07 9:32 
QuestionWhat kind of listview do you use Pin
ratel330-Apr-05 20:06
ratel330-Apr-05 20:06 
AnswerRe: What kind of listview do you use Pin
Thommy Mewes1-May-05 10:34
Thommy Mewes1-May-05 10:34 
AnswerRe: What kind of listview do you use Pin
ratel35-May-05 2:42
ratel35-May-05 2:42 
QuestionID3V2 support? Pin
EdwardA30-Mar-05 8:07
EdwardA30-Mar-05 8:07 
AnswerRe: ID3V2 support? Pin
Thommy Mewes30-Mar-05 8:53
Thommy Mewes30-Mar-05 8:53 
GeneralMessage Closed Pin
23-May-05 7:10
DomGries23-May-05 7:10 
GeneralRe: ID3V2 support? Pin
Anonymous27-May-05 22:51
Anonymous27-May-05 22:51 
GeneralRe: ID3V2 support? Pin
Thommy Mewes16-Jun-05 12:22
Thommy Mewes16-Jun-05 12:22 
QuestionRe: ID3V2 support? Pin
crouchie0418-Oct-05 12:44
crouchie0418-Oct-05 12:44 
AnswerRe: ID3V2 support? Pin
emkarwin29-Aug-07 21:57
emkarwin29-Aug-07 21:57 

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.