MP3 properties

eagle

Active member
Joined
Mar 8, 2005
Messages
28
Programming Experience
1-3
Does anybody know how you can get the properties of an mp3 file in an easy way? And with properties I mean the Album name, the song name, the artist and the duration of the song.



Thx
 

Lycaon

New member
Joined
Apr 20, 2005
Messages
3
Programming Experience
5-10
You'rer looking for the ID3v1 tag. This tag, if there's one in the MP3, is the last 128 bytes of the MP3. Here's some code to read it, feel free to modify for your own use. You can get rid of the Identifier and ZBS members of the IDTagv11 structure, I just have them there for completeness. They're never actually used anywhere in the code.

VB.NET:
 Public Structure IDTagv11 
	Dim Identifier As String ' 0 3 "TAG" identifier string.
	Dim Title As String ' 3 30 Song title string.
	Dim Artist As String ' 33 30 Artist string.
	Dim Album As String ' 63 30 Album string.
	Dim Year As String ' 93 4 Year string.
	Dim Comment As String ' 97 28 Comment string.
	Dim ZBS As Byte ' 125 1 Zero byte separator.
	Dim Track As Byte ' 126 1 Track byte.
	Dim Genre As Byte ' 127 1 Genre byte. 
End Structure
 
Dim fs As New System.IO.FileStream(File, IO.FileMode.Open)
Dim Tag(127) As Byte, idv As New IDTagv11
Dim enc As New System.Text.ASCIIEncoding
 
fs.Position = fi.Size - 128
fs.Read(Tag, 0, Tag.Length)
 
fs.Close()
 
If enc.GetString(Tag, 0, 3) = "TAG" Then
 
	idv.Identifier = enc.GetString(Tag, 0, 3) ' 0 3 "TAG" identifier string.
 
	idv.Title = enc.GetString(Tag, 3, 30).Trim ' 3 30 Song title string.
	If idv.Title.EndsWith(Chr(0)) Then idv.Title = idv.Title.Substring(0, idv.Title.IndexOf(Chr(0)))
 
	idv.Artist = enc.GetString(Tag, 33, 30).Trim ' 33 30 Artist string. 
	If idv.Artist.EndsWith(Chr(0)) Then idv.Artist = idv.Artist.Substring(0, idv.Artist.IndexOf(Chr(0)))
 
	idv.Album = enc.GetString(Tag, 63, 30).Trim ' 63 30 Album string. 
	If idv.Album.EndsWith(Chr(0)) Then idv.Album = idv.Album.Substring(0, idv.Album.IndexOf(Chr(0)))
 
	idv.Year = enc.GetString(Tag, 93, 4) ' 93 4 Year string. 
	If idv.Year.EndsWith(Chr(0)) Then idv.Year = idv.Year.Substring(0, idv.Year.IndexOf(Chr(0)))
 
	idv.Comment = enc.GetString(Tag, 97, 28).Trim ' 97 28 Comment string.
	If idv.Comment.EndsWith(Chr(0)) Then idv.Comment = idv.Comment.Substring(0, idv.Comment.IndexOf(Chr(0)))
 
	idv.ZBS = Tag(125) ' 125 1 Zero byte separator.
	idv.Track = Tag(126) ' 126 1 Track byte. 
	idv.Genre = Tag(127) ' 127 1 Genre byte.
 
End If

 
Top Bottom