Click here to Skip to main content
Click here to Skip to main content

Read MP3 header information and read/write the ID3v1 tag

By , 1 Mar 2005
 

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:

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

About the Author

Thommy Mewes
Software Developer (Senior) CIBER AG
Germany Germany
Member
Feel free to contact me via Email or MSN Messenger (thommy@live.com).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionAlbum artmemberraghubirdevnet16 Oct '12 - 0:02 
what about mp3 artwork?
how to read them ?
ZaneRsoft Corporation

GeneralCRC Protection?memberMember 357297015 May '11 - 14:19 
Does the source code get the CRC after the header (if their is one)? Then calculate what the CRC should be and compare it to the one read after the header for MPEG Frame Audio Header validation?
GeneralIDv2memberSousuke_31 Aug '10 - 10:35 
How going works on it?
QuestionWie kann ich Änderungen speichern? MP3membermichaelroswitha13 Jan '10 - 8:25 
With MP3Info.ID3v1Tag
.Title = DataGridViewMp3.Rows(e.RowIndex).Cells(4).Value
.Artist = DataGridViewMp3.Rows(e.RowIndex).Cells(3).Value
.Album = DataGridViewMp3.Rows(e.RowIndex).Cells(5).Value
.Year = DataGridViewMp3.Rows(e.RowIndex).Cells(6).Value
.Genre = 9
End With
MP3Info.Update()--> Funktioniert nicht
QuestionHow to read the actual genre string when it is "other"membera1penguin18 Jun '09 - 17:05 
I have an application where all of the mp3 files will have made up genres which are not in the list of enums. In all cases I get 12 when I read the genre or "other" when I read the string. Is there a way to read back the actual genre string in these cases.
GeneralVBR Filemembernitesh gaba19 Mar '09 - 21:30 
It doesnot give correct file duration of VBR file. Whereas this library gives http://www.codeproject.com/KB/audio-video/mpegaudioinfo.aspx[^] correct length after reading complete file.
 
Could you please help me.
QuestionWhat i write to the tag doesn't show up when viewing the property details in the file system.memberMattRoy1018 Mar '09 - 14:11 
Reading works fine. When i write to the tag, the value doesn't appear to change if it already contained a value. However, if i proceed to read the same information it returns what i previously wrote.
 
Example:
The MP3 ID tag already contains the album name "Original Album" as seen in the file system. I write the new album info like this:
 
objMP3Info.ID3v1Tag.Album = "New Album"
objMP3Info.Update()
 
If i read the tag again, i will see "New Album", but if i view the file properties i don't see the change. If i clear the tag using the file system (Windows Vista) and re-run the code i will get the results i expect. The new album name will be written and viewed correctly in the file system.
 
Basically, i need to manually clear out all the tag info if i want this sample code to write values that i will see with my OS.
 
Please help!
AnswerRe: What i write to the tag doesn't show up when viewing the property details in the file system.membermazin221 Dec '10 - 13:11 
you should use objMP3Info.ID3v1Tag.Update()
but 'Update' is not a member of 'Monotic.Multimedia.MP3.ID3v1Tag'.
we all are waiting for an answer.
Generalhip hop songs and dancememberJigga Hov12 Dec '08 - 5:58 
I am looking for a fresh version of the ID with words of the songs. It is extremely useful in hip hop songs.
 
Also while looking at website about hip hop dance moves I have found that the old tags might do the job because there are less words at the songs. Blush | :O
GeneralGNU OpenSource Tag Library for .NETmembersomeonestupid196910 Dec '08 - 4:55 
http://developer.novell.com/wiki/index.php/TagLib_Sharp
 
by the way, not by me... I am just using it... Cool | :cool:

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 1 Mar 2005
Article Copyright 2005 by Thommy Mewes
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid