Important alert: (current site time 6/19/2013 9:49:34 PM EDT)
 

VB icon

How to read/write MP3 ID3-Tags with UltraID3Lib

Email
Submitted on: 8/6/2012 3:53:44 PM
By: C. Sibon  
Level: Beginner
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 3120
 
     Here is an example of how to use the most popular MP3 ID3-Tag reader/writer, UltraID3Lib (version 1 up to v2.3) UltraID3Lib, written in VB.Net Express (2010). However, the source code is also available in C# see links in source code. In the example you can find how to easily handle the most common frames in mp3 tagging but, with very low effort can be extended beyond this example. 1. Copy/Paste the source code into a cla 2. Example how to read title from tag. In your form paste the following: Private Sub ReadID3(byval strFileName as string) Dim cls_ID3 As New clsID3_EXT Dim objPROP As New strucRet objPROP = cls_ID3.Read(strFileName) msgbox(cls_ID3.Artist) end sub call the sub, reference the path to an mp3 file.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: How to read/write MP3 ID3-Tags with UltraID3Lib
// Description:Here is an example of how to use the most popular MP3 ID3-Tag reader/writer, UltraID3Lib (version 1 up to v2.3) UltraID3Lib, written in VB.Net Express (2010). However, the source code is also available in C# see links in source code.
In the example you can find how to easily handle the most common frames in mp3 tagging but, with very low effort can be extended beyond this example.
1. Copy/Paste the source code into a cla
2. Example how to read title from tag. In your form paste the following:
Private Sub ReadID3(byval strFileName as string)
Dim cls_ID3 As New clsID3_EXT
Dim objPROP As New strucRet
objPROP = cls_ID3.Read(strFileName)
msgbox(cls_ID3.Artist)
end sub
call the sub, reference the path to an mp3 file.
// By: C. Sibon
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=8825&lngWId=10//for details.//**************************************

Imports System.Drawing
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.IO
Imports System.ComponentModel
Imports HundredMilesSoftware.UltraID3Lib
Imports System.Text.RegularExpressions
Public Structure strucRet
Dim blnERROR As Boolean
Dim strErr_msg As String
End Structure
'Original source code written in C#: http://code.google.com/p/music-database/source/browse/trunk/libdb/tag.cs?r=33
' EXAMPLE HOW TO READ/WRITE PICTURES (COVERS etc.) see UltraID3Lib.dll homepage for UltraID3Lib Test Harness
'THIS EXAMPLE REQUIRES UltraID3Lib.dll; download here http://home.fuse.net/honnert/UltraID3Lib/ used version v.0.9.6.8 (2010)
Private u As New UltraID3()
Private UserText As New System.Collections.Specialized.NameValueCollection()
Private _Comments As String = ""
Private _DBInfo As String = ""
Public Sub New()
End Sub
Public Sub New(Filepath As String)
Me.New()
Me.Read(Filepath)
End Sub
Public Function Read(ByVal Filepath As String) As strucRet
Dim objPROP As New strucRet
Try
Dim chkFile As New FileInfo(Filepath)
objPROP.blnERROR = True
If chkFile.Exists Then
If chkFile.Length > 0 Then
u.Read(Filepath)
Me.UserText.Clear()
For Each fr As ID3v23UserDefinedTextFrame In u.ID3v2Tag.Frames.GetFrames(MultipleInstanceID3v2FrameTypes.ID3v23UserDefinedText)
Me.UserText.Add(fr.Description, fr.UserDefinedText)
Next
Me._Comments = ""
Me._DBInfo = ""
For Each c As ID3v23CommentsFrame In u.ID3v2Tag.Frames.GetFrames(MultipleInstanceID3v2FrameTypes.ID3v23Comments)
' TODO: exclude some other comments such as itune stuff
If c.Description <> "DBInfo" And Not String.IsNullOrEmpty(c.Comments.Trim()) Then
Me._Comments += (If(String.IsNullOrEmpty(Me._Comments), "", vbLf)) + c.Comments
ElseIf c.Description = "DBInfo" Then
Me._DBInfo = c.Comments
End If
Next
'/u.ID3v2Tag.WillWrite = true;
'/u.ID3v1Tag.WillWrite = false;
u.ID3v2Tag.Frames.AddNewFrameTextEncodingType = TextEncodingTypes.Unicode
objPROP.blnERROR = False
Else
objPROP.strErr_msg = "File is empty."
End If
Else
objPROP.strErr_msg = "File not found."
End If
Catch ex As Exception
objPROP.strErr_msg = ex.Message
'MsgBox("Error from clsID3_EXT.Read function: " & ex.Message, MsgBoxStyle.OkOnly)
End Try
Return objPROP
End Function
Public Sub Write()
u.ID3v2Tag.Frames.Remove(ID3v2FrameTypes.ID3v23UserDefinedText)
Dim n As ID3v23UserDefinedTextFrame = Nothing
Dim s As New System.Text.StringBuilder()
For i As Integer = 0 To Me.UserText.Count - 1
n = New ID3v23UserDefinedTextFrame(Me.UserText.GetValues(i)(0))
n.Description = Me.UserText.GetKey(i)
'n.UserDefinedText = this.UserText.GetValues(i)[0];
u.ID3v2Tag.Frames.Add(n)
s.Append(n.Description + ":" + n.UserDefinedText)
Next
u.ID3v2Tag.Frames.SetComments("DBInfo", s.ToString())
u.Write()
End Sub
Public Sub Clear()
u.ID3v1Tag.Clear()
u.ID3v2Tag.Clear()
u.Clear()
'u.ID3v2Tag.WillWrite = false;
'u.ID3v1Tag.WillWrite = false;
u.Write()
' okay dirty fix since the lib does not seem to actually Clear() the tag.
'u.ID3v2Tag.WillWrite = true;
End Sub
Public ReadOnly Property UltraID3() As UltraID3
Get
Return u
End Get
End Property
Public Property Album() As String
Get
If Not String.IsNullOrEmpty(u.ID3v2Tag.Album) Then
Return u.ID3v2Tag.Album
ElseIf Not String.IsNullOrEmpty(u.ID3v1Tag.Album) Then
Return u.ID3v1Tag.Album
Else
Return Nothing
End If
End Get
Set(value As String)
u.ID3v2Tag.Frames.Remove(ID3v2FrameTypes.ID3v23Album)
If Not String.IsNullOrEmpty(value) Then
u.ID3v2Tag.Frames.Add(New ID3v23AlbumFrame(value))
End If
'also for ID3v1:
Dim strAlbum As String
strAlbum = value
If value.Length > 30 Then
strAlbum = strAlbum.Substring(0, 30)
End If
u.ID3v1Tag.Album = strAlbum
End Set
End Property
Public Property Composer() As String()
Get
If Not String.IsNullOrEmpty(u.ID3v2Tag.Artist) Then
Return u.ID3v2Tag.Artist.Split(New Char() {"/"c}, System.StringSplitOptions.RemoveEmptyEntries)
ElseIf Not String.IsNullOrEmpty(u.ID3v1Tag.Artist) Then
Return u.ID3v1Tag.Artist.Split(New Char() {"/"c}, System.StringSplitOptions.RemoveEmptyEntries)
Else
Return Nothing
End If
End Get
Set(value As String())
u.ID3v2Tag.Frames.Remove(ID3v2FrameTypes.ID3v23Artist)
If value IsNot Nothing AndAlso value.Length > 0 Then
u.ID3v2Tag.Frames.Add(New ID3v23ArtistFrame(String.Join("/", value)))
End If
End Set
End Property
Public Property Title() As String
Get
If Not String.IsNullOrEmpty(u.ID3v2Tag.Title) Then
Return u.ID3v2Tag.Title
ElseIf Not String.IsNullOrEmpty(u.ID3v1Tag.Title) Then
Return u.ID3v1Tag.Title
Else
Return Nothing
End If
End Get
Set(value As String)
u.ID3v2Tag.Frames.Remove(ID3v2FrameTypes.ID3v23Title)
If Not String.IsNullOrEmpty(value) Then
u.ID3v2Tag.Frames.Add(New ID3v23TitleFrame(value))
End If
'also for ID3v1:
Dim strTitle As String
strTitle = value
If value.Length > 30 Then
strTitle = strTitle.Substring(0, 30)
End If
u.ID3v1Tag.Title = strTitle
End Set
End Property
Public Property Genre() As String
Get
If Not String.IsNullOrEmpty(u.ID3v2Tag.Genre) Then
Return u.ID3v2Tag.Genre
ElseIf Not String.IsNullOrEmpty(u.ID3v1Tag.GenreName) Then
Return u.ID3v1Tag.GenreName
Else
Return Nothing
End If
End Get
Set(value As String)
u.ID3v2Tag.Frames.Remove(ID3v2FrameTypes.ID3v23Genre)
If Not String.IsNullOrEmpty(value) Then
u.ID3v2Tag.Frames.Add(New ID3v23GenreFrame(value))
End If
'also for ID3v1:
Dim intGenre As Integer
If Not String.IsNullOrEmpty(value) Then
intGenre = -1
For Each IndexGenreInfo As GenreInfo In u.GenreInfos
If IndexGenreInfo.Number <> 255 Then
If Genre.ToLower = IndexGenreInfo.Name.ToLower Then
intGenre = IndexGenreInfo.Number
End If
End If
Next
If intGenre > -1 Then
u.ID3v1Tag.Genre = intGenre
End If
End If
End Set
End Property
' public int? GenreID {
' get {
' if (this.Genre == null) return null;
' int i = MusicDataBase.Genre.GetGenreID(this.Genre);
' return (i != 0) ? i : null;
' }
' set {
' u.ID3v2Tag.Frames.Remove(ID3v2FrameTypes.ID3v23Genre);
' if ((int)value != 0) u.ID3v2Tag.Frames.Add(new ID3v23GenreFrame(MusicDataBase.Genre.GetGenreName(value)));
' }
' }
Public Property AlbumArtist() As String
Get
Return If((u.ID3v2Tag.Frames.GetFrame(SingleInstanceID3v2FrameTypes.ID3v23Band) IsNot Nothing), DirectCast(u.ID3v2Tag.Frames.GetFrame(SingleInstanceID3v2FrameTypes.ID3v23Band), ID3v23BandFrame).Band, Nothing)
End Get
Set(value As String)
If Not String.IsNullOrEmpty(value) Then
u.ID3v2Tag.Frames.Remove(ID3v2FrameTypes.ID3v23Band)
u.ID3v2Tag.Frames.Add(New ID3v23BandFrame(value))
End If
End Set
End Property
Public Property Artist() As String
Get
Return If((u.ID3v2Tag.Frames.GetFrame(CommonSingleInstanceID3v2FrameTypes.Artist) IsNot Nothing), DirectCast(u.ID3v2Tag.Frames.GetFrame(CommonSingleInstanceID3v2FrameTypes.Artist), ID3v23ArtistFrame).Artist, Nothing)
End Get
Set(value As String)
If Not String.IsNullOrEmpty(value) Then
u.ID3v2Tag.Frames.Remove(CommonID3v2FrameTypes.Artist)
u.ID3v2Tag.Frames.Add(New ID3v23ArtistFrame(value))
End If
'also for ID3v1:
Dim strArtist As String
strArtist = value
If value.Length > 30 Then
strArtist = strArtist.Substring(0, 30)
End If
u.ID3v1Tag.Artist = strArtist
End Set
End Property
Public Property Label() As String
Get
Return If(u.ID3v2Tag.Frames.GetFrame(SingleInstanceID3v2FrameTypes.ID3v23Publisher) IsNot Nothing, DirectCast(u.ID3v2Tag.Frames.GetFrame(SingleInstanceID3v2FrameTypes.ID3v23Publisher), ID3v23PublisherFrame).Publisher, Nothing)
End Get
Set(value As String)
If Not String.IsNullOrEmpty(value) Then
u.ID3v2Tag.Frames.Remove(ID3v2FrameTypes.ID3v23Publisher)
u.ID3v2Tag.Frames.Add(New ID3v23PublisherFrame(value))
End If
End Set
End Property
Public Property ContentGroup() As String
Get
Return If(u.ID3v2Tag.Frames.GetFrame(SingleInstanceID3v2FrameTypes.ID3v23ContentGroupDescription) IsNot Nothing, DirectCast(u.ID3v2Tag.Frames.GetFrame(SingleInstanceID3v2FrameTypes.ID3v23ContentGroupDescription), ID3v23ContentGroupDescriptionFrame).ContentGroupDescription, Nothing)
End Get
Set(value As String)
If Not String.IsNullOrEmpty(value) Then
u.ID3v2Tag.Frames.Remove(ID3v2FrameTypes.ID3v23ContentGroupDescription)
u.ID3v2Tag.Frames.Add(New ID3v23ContentGroupDescriptionFrame(value))
End If
End Set
End Property
Public ReadOnly Property TrackNum() As System.Nullable(Of Integer)
Get
Return If(u.ID3v2Tag.Frames.GetFrame(SingleInstanceID3v2FrameTypes.ID3v23TrackNum) IsNot Nothing, DirectCast(u.ID3v2Tag.Frames.GetFrame(SingleInstanceID3v2FrameTypes.ID3v23TrackNum), ID3v23TrackNumFrame).TrackNum, Nothing)
End Get
End Property
Public ReadOnly Property TotalTracks() As System.Nullable(Of Integer)
Get
Return If(u.ID3v2Tag.Frames.GetFrame(SingleInstanceID3v2FrameTypes.ID3v23TrackNum) IsNot Nothing, DirectCast(u.ID3v2Tag.Frames.GetFrame(SingleInstanceID3v2FrameTypes.ID3v23TrackNum), ID3v23TrackNumFrame).TrackCount, Nothing)
End Get
End Property
'Public Sub SetTrack(TrackNum As String, TotalTrack As String)
'u.ID3v2Tag.Frames.Remove(ID3v2FrameTypes.ID3v23TrackNum)
'If TrackNum = 0 And TotalTrack = 0 Then
'Return
'End If
'If TotalTrack <> 0 Then
'u.ID3v2Tag.Frames.Add(New ID3v23TrackNumFrame(TrackNum, TotalTrack))
'Else
'u.ID3v2Tag.Frames.Add(New ID3v23TrackNumFrame(TrackNum))
'End If
'End Sub
Public Sub SetTrack(TrackNum As String)
u.ID3v2Tag.Frames.Remove(ID3v2FrameTypes.ID3v23TrackNum)
If TrackNum = 0 Then
Return
End If
'u.ID3v2Tag.Frames.Add(New ID3v23TrackNumFrame(CByte(TrackNum)))'changed to byval string such to accept leading zeroes ie: #01 also cbyte max 255! = fix
u.ID3v2Tag.Frames.Add(New ID3v23TrackNumFrame(TrackNum))
'also for ID3v1:
u.ID3v1Tag.SetTrackNum(TrackNum)
End Sub
Public ReadOnly Property DiscNum() As System.Nullable(Of Integer)
Get
Return If(u.ID3v2Tag.Frames.GetFrame(SingleInstanceID3v2FrameTypes.ID3v23PartOfSet) IsNot Nothing, CType(DirectCast(u.ID3v2Tag.Frames.GetFrame(SingleInstanceID3v2FrameTypes.ID3v23PartOfSet), ID3v23PartOfSetFrame).PartNum, System.Nullable(Of Integer)), Nothing)
End Get
End Property
Public ReadOnly Property DiscCount() As System.Nullable(Of Integer)
Get
Return If(u.ID3v2Tag.Frames.GetFrame(SingleInstanceID3v2FrameTypes.ID3v23PartOfSet) IsNot Nothing, CType(DirectCast(u.ID3v2Tag.Frames.GetFrame(SingleInstanceID3v2FrameTypes.ID3v23PartOfSet), ID3v23PartOfSetFrame).PartCount, System.Nullable(Of Integer)), Nothing)
End Get
End Property
Public Sub SetDiscNum(DiscNum As Integer, TotalDisc As Integer)
u.ID3v2Tag.Frames.Remove(ID3v2FrameTypes.ID3v23PartOfSet)
If DiscNum = 0 And TotalDisc = 0 Then
Return
End If
If TotalDisc <> 0 Then
u.ID3v2Tag.Frames.Add(New ID3v23PartOfSetFrame(CByte(DiscNum), CByte(TotalDisc)))
Else
u.ID3v2Tag.Frames.Add(New ID3v23PartOfSetFrame(CByte(DiscNum)))
End If
End Sub
Public Property Year() As System.Nullable(Of Integer)
Get
Return u.ID3v2Tag.Year
End Get
Set(value As System.Nullable(Of Integer))
u.ID3v2Tag.Frames.Remove(ID3v2FrameTypes.ID3v23Year)
If value.HasValue AndAlso value.Value <> 0 Then
u.ID3v2Tag.Frames.Add(New ID3v23YearFrame(CShort(value.Value)))
End If
'also for ID3v1:
u.ID3v1Tag.Year = value
End Set
End Property
Public Property Performers() As String()
Get
Dim a As String() = New String(DirectCast(u.ID3v2Tag.Frames.GetFrame(SingleInstanceID3v2FrameTypes.ID3v23Composers), ID3v23ComposersFrame).Composers.Count - 1) {}
For i As Integer = 0 To a.GetUpperBound(0)
a(i) = DirectCast(u.ID3v2Tag.Frames.GetFrame(SingleInstanceID3v2FrameTypes.ID3v23Composers), ID3v23ComposersFrame).Composers(i)
Next
Return a
End Get
Set(value As String())
Dim c As New ID3v23ComposersFrame()
For i As Integer = 0 To value.Length - 1
c.Composers.Add(value(i))
Next
u.ID3v2Tag.Frames.Add(c)
End Set
End Property
Public Property AlbumPicture() As Bitmap
Get
Dim p As ID3v23PictureFrame = DirectCast(u.ID3v2Tag.Frames.GetFrames(MultipleInstanceID3v2FrameTypes.ID3v23Picture)(0), ID3v23PictureFrame)
Return If((p IsNot Nothing), p.Picture, Nothing)
End Get
Set(value As Bitmap)
u.ID3v2Tag.Frames.Remove(ID3v2FrameTypes.ID3v23Picture)
If value IsNot Nothing Then
u.ID3v2Tag.Frames.Add(New ID3v23PictureFrame(value, PictureTypes.CoverFront, "", TextEncodingTypes.Unicode))
End If
End Set
End Property
Public Property UnsynchedLyric() As String
Get
Return DirectCast(u.ID3v2Tag.Frames.GetFrames(MultipleInstanceID3v2FrameTypes.ID3v23UnsyncedLyrics)(0), ID3v23UnsynchedLyricsFrame).UnsynchedLyrics
End Get
Set(value As String)
u.ID3v2Tag.Frames.Remove(ID3v2FrameTypes.ID3v23UnsyncedLyrics)
If value IsNot Nothing And Not String.IsNullOrEmpty(value) Then
u.ID3v2Tag.Frames.Add(New ID3v23UnsynchedLyricsFrame(value))
End If
End Set
End Property
' public string UserDefinedText (string Description) {
' get { return this.UserText.Get(Description); }
' set {
' this.UserText.Remove(Description);
' if (!string.IsNullOrEmpty(value)) {
' this.UserText.Add(Description, value);
' }
' }
Public Property Comment() As String
Get
Return Me._Comments
End Get
Set(value As String)
value = value.Trim()
If Not String.IsNullOrEmpty(value) Then
Me._Comments = value
u.ID3v2Tag.Frames.Remove(ID3v2FrameTypes.ID3v23Comments)
u.ID3v2Tag.Frames.Add(New ID3v23CommentsFrame(value, ""))
End If
End Set
End Property
Private Function DBInfoFromComment(key As String) As String
If Me._DBInfo.ToUpper().Contains(key.ToUpper()) Then
Dim i As Integer = Me._DBInfo.ToUpper().IndexOf(":", _DBInfo.ToUpper().IndexOf(key.ToUpper(), 0)) + 1
Dim j As Integer = Me._DBInfo.ToUpper().IndexOf("X", i)
If j = -1 Then
j = Me._DBInfo.Length
End If
Return Me._DBInfo.Substring(i, j - i)
Else
Return Nothing
End If
End Function
Private Function DBInfo(key As String) As System.Nullable(Of Integer)
If Not String.IsNullOrEmpty(Me.UserText.[Get](key)) Then
Return Integer.Parse(UserText.[Get](key))
ElseIf Not String.IsNullOrEmpty(Me.DBInfoFromComment(key)) Then
Return Integer.Parse(Me.DBInfoFromComment(key))
Else
Return Nothing
End If
End Function
Public Property AlbumID() As System.Nullable(Of Integer)
Get
If Me.DBInfo("xAlbumID").HasValue Then
Return CInt(Me.DBInfo("xAlbumID"))
Else
Return Nothing
End If
End Get
Set(value As System.Nullable(Of Integer))
Me.UserText.Remove("xAlbumID")
If value.HasValue AndAlso value.Value <> 0 Then
Me.UserText.Add("xAlbumID", value.Value.ToString())
End If
End Set
End Property
'Public Property AlbumType() As System.Nullable(Of AlbumType)
'Get
'If Me.DBInfo("xAlbumType").HasValue Then
'Return DirectCast(Me.DBInfo("xAlbumType").Value, AlbumType)
'ElseIf Me.DBInfo("!AlbumType").HasValue Then
'Return DirectCast(Me.DBInfo("!AlbumType").Value, AlbumType)
'ElseIf u.FileName.Contains("Various Artists") Then
'Return AlbumType.VariousArtistAlbum
'ElseIf u.FileName.Contains("Collection I") Then
'Return AlbumType.ComposerAlbum
'ElseIf u.FileName.Contains("Collection II") Then
'Return AlbumType.ArtistAlbum
'Else
'Return Nothing
'End If
'End Get
'Set(value As System.Nullable(Of AlbumType))
'Me.UserText.Remove("xAlbumType")
'Me.UserText.Add("xAlbumType", CInt(value).ToString())
'End Set
'End Property
Public Property TrackID() As System.Nullable(Of Integer)
Get
If Me.DBInfo("xTrackID").HasValue Then
Return CInt(Me.DBInfo("xTrackID"))
Else
Return Nothing
End If
End Get
Set(value As System.Nullable(Of Integer))
Me.UserText.Remove("xTrackID")
If value.HasValue AndAlso value.Value <> 0 Then
Me.UserText.Add("xTrackID", value.Value.ToString())
End If
End Set
End Property
Public Property IsComplete() As System.Nullable(Of Boolean)
Get
If Me.DBInfo("xCompleteAlbum").HasValue Then
Return If((Me.DBInfo("xCompleteAlbum").Value = 0), False, True)
Else
Return Nothing
End If
End Get
Set(value As System.Nullable(Of Boolean))
Me.UserText.Remove("xCompleteAlbum")
If value.HasValue Then
Me.UserText.Add("xCompleteAlbum", If(value.Value, "1", "0"))
End If
End Set
End Property
Public Property IsInIpod() As System.Nullable(Of Boolean)
Get
If Me.DBInfo("xiPod").HasValue Then
Return If((DBInfo("xiPod").Value = 0), True, False)
Else
Return Nothing
End If
End Get
Set(value As System.Nullable(Of Boolean))
Me.UserText.Remove("xiPod")
If value.HasValue Then
Me.UserText.Add("xiPod", If(value.Value, "1", "0"))
End If
End Set
End Property


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.