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

Reading and writing MP3 ID3v1 tags

Rate me:
Please Sign up or sign in to vote.
4.64/5 (29 votes)
4 Dec 2003 212.4K   4K   68   36
A simple class for mainpulating ID3v1 tags of MP3 files.

Introduction

This article provides a simple class which can read and write MP3 ID3v1 tags.

The code

The code itself is very easy to understand, here it is:

VB.NET
Imports System.IO

Public Class MP3ID3v1

    ' Constructor
    Public Sub New(Optional ByVal Filename As String = "")
        MyBase.New()
        If (Filename <> "") Then Me.Filename = Filename
    End Sub

    ' Genres
    Public Enum Genres As Byte
        Blues = 0
        ClassicRock = 1
        Country = 2
        Dance = 3
        Disco = 4
        Funk = 5
        Grunge = 6
        HipHop = 7
        Jazz = 8
        Metal = 9
        NewAge = 10
        Oldies = 11
        Other = 12
        Pop = 13
        RnB = 14
        Rap = 15
        Reggae = 16
        Rock = 17
        Techno = 18
        Industrial = 19
        Alternative = 20
        Ska = 21
        DeathMetal = 22
        Pranks = 23
        Soundtrack = 24
        EuroTechno = 25
        Ambient = 26
        TripHop = 27
        Vocal = 28
        JazzFunk = 29
        Fusion = 30
        Trance = 31
        Classical = 32
        Instrumental = 33
        Acid = 34
        House = 35
        Game = 36
        SoundClip = 37
        Gospel = 38
        Noise = 39
        AlternRock = 40
        Bass = 41
        Soul = 42
        Punk = 43
        Space = 44
        Meditative = 45
        InstrumentalPop = 46
        InstrumentalRock = 47
        Ethnic = 48
        Gothic = 49
        Darkwave = 50
        TechnoIndustrial = 51
        Electronic = 52
        PopFolk = 53
        Eurodance = 54
        Dream = 55
        SouthernRock = 56
        Comedy = 57
        Cult = 58
        Gangsta = 59
        Top40 = 60
        ChristianRap = 61
        PopFunk = 62
        Jungle = 63
        NativeAmerican = 64
        Cabaret = 65
        NewWave = 66
        Psychadelic = 67
        Rave = 68
        Showtunes = 69
        Trailer = 70
        LoFi = 71
        Tribal = 72
        AcidPunk = 73
        AcidJazz = 74
        Polka = 75
        Retro = 76
        Musical = 77
        RocknRoll = 78
        HardRock = 79
        None = 255
    End Enum

    ' Frame types
    Public Enum FrameTypes As Byte
        Title = 0
        Artist = 1
        Album = 2
        Year = 3
        Track = 4
        Comment = 5
        Genre = 6
    End Enum

    ' Filename
    Private mstrFilename As String
    Public Property Filename() As String
        Get
            Return mstrFilename
        End Get
        Set(ByVal Value As String)
            Dim objFile As File
            If (objFile.Exists(Value)) Then
                mstrFilename = Value
                Refresh()
            Else
                Throw New System.IO.FileLoadException( _
                    "The specified file does not exist", Value)
            End If
        End Set
    End Property

    ' TagExists
    Private mblnTagExists As Boolean
    Public ReadOnly Property TagExists() As Boolean
        Get
            Return mblnTagExists
        End Get
    End Property

    ' Frame
    Private mobjFrame(7) As Object
    Public Property Frame(ByVal FrameType As FrameTypes)
        Get
            Return mobjFrame(FrameType)
        End Get
        Set(ByVal Value)
            mobjFrame(FrameType) = Value
        End Set
    End Property

    ' Refresh (gets all tags from the specified file)
    Public Sub Refresh()

        ' Declarations
        Dim strTag As New String(" ", 3)
        Dim strTitle As New String(" ", 30)
        Dim strArtist As New String(" ", 30)
        Dim strAlbum As New String(" ", 30)
        Dim strYear As New String(" ", 4)
        Dim strComment As New String(" ", 28)
        Dim bytDummy As Byte
        Dim bytTrack As Byte
        Dim bytGenre As Byte

        ' Open the file
        Dim intFile As Integer = FreeFile()
        FileOpen(intFile, mstrFilename, OpenMode.Binary, _
              OpenAccess.Read, OpenShare.LockWrite)

        ' Gets length of file
        Dim lngLOF As Long = LOF(intFile)
        If (lngLOF > 128) Then

            ' Check for the ID3v1 tag
            FileGet(intFile, strTag, lngLOF - 127, True)
            If (strTag.ToUpper <> "TAG") Then

                ' No ID3v1 tag found
                mblnTagExists = False
                mobjFrame(0) = ""
                mobjFrame(1) = ""
                mobjFrame(2) = ""
                mobjFrame(3) = ""
                mobjFrame(4) = ""
                mobjFrame(5) = ""
                mobjFrame(6) = ""

            Else

                ' ID3v1 tag found
                mblnTagExists = True

                ' Read all frames from the file
                FileGet(intFile, strTitle)
                FileGet(intFile, strArtist)
                FileGet(intFile, strAlbum)
                FileGet(intFile, strYear)
                FileGet(intFile, strComment)
                FileGet(intFile, bytDummy)
                FileGet(intFile, bytTrack)
                FileGet(intFile, bytGenre)

                ' Assign the frame content to the properties
                mobjFrame(0) = strTitle
                mobjFrame(1) = strArtist
                mobjFrame(2) = strAlbum
                mobjFrame(3) = strYear
                mobjFrame(4) = bytTrack
                mobjFrame(5) = strComment
                mobjFrame(6) = bytGenre

            End If
        End If

        ' Close the file
        FileClose(intFile)

    End Sub

    ' Update
    Public Sub Update()

        ' Declarations
        Dim strTag As New String(" ", 3)
        Dim strTitle As New String(" ", 30)
        Dim strArtist As New String(" ", 30)
        Dim strAlbum As New String(" ", 30)
        Dim strYear As New String(" ", 4)
        Dim strComment As New String(" ", 28)
        Dim bytDummy As Byte
        Dim bytTrack As Byte
        Dim bytGenre As Byte

        ' Open the file
        Dim intFile As Integer = FreeFile()
        FileOpen(intFile, mstrFilename, OpenMode.Binary, _
           OpenAccess.ReadWrite, OpenShare.LockWrite)

        ' Gets length of file
        Dim lngLOF As Long = LOF(intFile)
        If (lngLOF > 0) Then
            If (lngLOF > 128) Then

                ' Check for an existing ID3v1 tag
                FileGet(intFile, strTag, lngLOF - 127)
                If (strTag.ToUpper <> "TAG") Then

                    ' No ID3v1 tag found, so just add one
                    Seek(intFile, lngLOF)
                    strTag = "TAG"
                    FilePut(intFile, strTag)

                End If

                ' Fix the length of the frames
                strTitle = LSet(mobjFrame(0), Len(strTitle))
                strArtist = LSet(mobjFrame(1), Len(strArtist))
                strAlbum = LSet(mobjFrame(2), Len(strAlbum))
                strYear = LSet(mobjFrame(3), Len(strYear))
                bytTrack = mobjFrame(4)
                strComment = LSet(mobjFrame(5), Len(strComment))
                bytGenre = mobjFrame(6)

                ' Write the frames to the file
                FilePut(intFile, strTitle)
                FilePut(intFile, strArtist)
                FilePut(intFile, strAlbum)
                FilePut(intFile, strYear)
                FilePut(intFile, strComment)
                FilePut(intFile, bytDummy)
                FilePut(intFile, bytTrack)
                FilePut(intFile, bytGenre)

            End If
        End If

        ' Close the file
        FileClose(intFile)

    End Sub

End Class

Using the code

To implement the class in your projects, just add the class file to your solution and declare a new instance of it:

VB.NET
Dim objMP3V1 As New MP3ID3v1("c:\song.mp3")

If the MP3 file contains tags, the property TagExists will be True, so you can start reading the tags (you'll find all frame types inside the code):

VB.NET
If (objMP3V1.TagExists) Then
    MessageBox.Show(objMP3V1.Frame(MP3ID3v1.FrameTypes.Album))
    MessageBox.Show(objMP3V1.Frame(MP3ID3v1.FrameTypes.Artist))
End If

For assigning a new tag, just set the property to the new value and call Update():

VB.NET
objMP3V1.Frame(MP3ID3v1.FrameTypes.Album) = "Album name"
objMP3V1.Update()

That's it.

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

 
QuestionNon Hard Coded MP3 file Pin
Skye Cairns from Unknown1-Apr-24 8:56
Skye Cairns from Unknown1-Apr-24 8:56 
QuestionDisplay mp3 tags on game screen (DirectX Windows) Pin
Marek Argent (Abrimaal)30-Dec-23 19:57
Marek Argent (Abrimaal)30-Dec-23 19:57 
QuestionImplementation Pin
gps587-Aug-18 22:48
gps587-Aug-18 22:48 
Great code! Thank you.
Here the other codes for your class:

Folk=080
FolkRock=081
NationalFolk=082
Swing=083
FastFusion=084
Bebop=085
Latin=086
Revival=087
Celtic=088
Bluegrass=089
Avantgarde=090
GothicRock=091
ProgressiveRock=092
PsychedelicRock=093
SymphonicRock=094
SlowRock=095
BigBand=096
Chorus=097
EasyListening=098
Acoustic=099
Humour=100
Speech=101
Chanson=102
Opera=103
ChamberMusic=104
Sonata=105
Symphony=106
BootyBass=107
Primus=108
PornGroove=109
Satire=110
SlowJam=111
Club=112
Tango=113
Samba=114
Folklore=115
Ballad=116
PowerBallad=117
RhytmicSoul=118
FreesStyle=119
Duet=120
PunkRock=121
DrumSolo=122
ACapella=123
EuroHouse=124
DanceHall=125
Goa=126
DrumNBass=127
ClubHouse=128
Hardcore=129
Terror=130
Indie=131
BritPop=132
NegerPunk=133
PolskPunk=134
Beat=135
ChristianGangsta=136
HeavyMetal=137
BlackMetal=138
CrossOver=139
ContemporaryChristian=140
ChristianRock=141
Merengue=142
Salsa=143
ThrashMetal=144
Anime=145
JPop=146
SynthPop=147
LagMusic=148
ArtRock=149
DeepHouse=150
TechHouse=151
DeepStep=152
DubStep=153
FutureHouse=154
SubGround=155
Trap=156
EBM=157
ProgressiveHouse=158
MelbourneBounce=159
DutchHouse=160
ElectroHouse=161
BoilerRoom=162
HardTekno=163
MinimalHouse=164
AmbientHouse=165
ChillOut=166
Dub=167
HardStyle=168
FreeStyle=169
DrumStep=170

GeneralMy vote of 1 Pin
Mikloo29-Dec-13 8:43
Mikloo29-Dec-13 8:43 
QuestionFull ID3 1.0, 2.0, 2.3, 2.4 Classes for VB.Net - Credits to Shad4Ever Pin
Mikloo29-Dec-13 8:41
Mikloo29-Dec-13 8:41 
AnswerRe: Full ID3 1.0, 2.0, 2.3, 2.4 Classes for VB.Net - Credits to Shad4Ever Pin
shad4ever15-Jul-14 15:58
shad4ever15-Jul-14 15:58 
AnswerRe: Full ID3 1.0, 2.0, 2.3, 2.4 Classes for VB.Net - Credits to Shad4Ever Pin
Mikloo21-Aug-14 17:18
Mikloo21-Aug-14 17:18 
QuestionAdding Tag Pin
davemas19-Oct-13 4:53
davemas19-Oct-13 4:53 
QuestionHow do I use this? Pin
critchm23-Aug-13 20:31
critchm23-Aug-13 20:31 
QuestionHI HOW AI GET THE SEGONDS FROM MP3? Pin
Ivan Silva Lemos4-Jul-13 13:28
Ivan Silva Lemos4-Jul-13 13:28 
SuggestionJapanese characters Pin
SousukeSagara16-Sep-12 16:04
SousukeSagara16-Sep-12 16:04 
QuestionWriting/Editing Track Duration of MP3 Pin
trC_27-Aug-12 13:33
trC_27-Aug-12 13:33 
GeneralMy vote of 4 Pin
Arthas_ls21-Nov-11 12:56
Arthas_ls21-Nov-11 12:56 
GeneralThank You! Pin
CircusUgly29-Apr-11 13:10
CircusUgly29-Apr-11 13:10 
Generaltag size Pin
Nitin Bhave19-Jul-10 18:55
Nitin Bhave19-Jul-10 18:55 
GeneralRe: tag size Pin
Zachary Lyons19-Feb-13 6:45
Zachary Lyons19-Feb-13 6:45 
GeneralRe: tag size Pin
Zachary Lyons19-Feb-13 9:43
Zachary Lyons19-Feb-13 9:43 
AnswerRe: tag size Pin
Zachary Lyons20-Feb-13 11:18
Zachary Lyons20-Feb-13 11:18 
QuestionCan you do in C#? Pin
rnbguru22-Jul-09 15:34
rnbguru22-Jul-09 15:34 
AnswerRe: Can you do in C#? Pin
Chuck Deluxe1-Aug-09 4:14
Chuck Deluxe1-Aug-09 4:14 
GeneralMy vote of 1 Pin
joseyoommen15-Jul-09 22:51
joseyoommen15-Jul-09 22:51 
GeneralImplementation Pin
Chuck Deluxe7-Jul-09 5:03
Chuck Deluxe7-Jul-09 5:03 
Generalhip hop music writing Pin
Just Blaaaze12-Dec-08 5:34
Just Blaaaze12-Dec-08 5:34 
Questiongetting error message Pin
alex-pof23-Jul-07 5:54
alex-pof23-Jul-07 5:54 
GeneralRe: getting error message Pin
Célio2-Mar-08 11:36
Célio2-Mar-08 11:36 

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.