![]() |
Languages »
VB.NET »
General
Intermediate
License: The Code Project Open License (CPOL)
MP3 ID3v1 EditorBy Hamed_jiEdit many ID3v1 at the same time |
VB, Windows, .NET, Visual Studio, Dev
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This application is for read and write ID3v1 information of mp3 files. As i explain the codes i explain how ID3v1 works.
ID3 is some part of mp3 files that contains information about that file. like the title of song or artist name and etc...
ID3 have 2 major versions and this program is for working with version one. and read or write ID3v1 data. for more information about ID3 visit http://www.id3.org/
Declare a buffer for read from file:
Public buffer(128) As Byte
ID3v1 is at the end of file and ID3v2 is at the begining of the file. so for open ID3v1 we need to read file from the end. Lenght of ID3v1 is fix and it's 128 bytes (it's because the buffer size is 128 bytes). Make a stream to file for read 128 bytes of file from end.
If mp3File.Length > 128 Then
Dim mp3Reader As Stream = mp3File.OpenRead()
mp3Reader.Seek(-128, SeekOrigin.End)
Dim i As Integer
For i = 0 To 127
buffer(i) = mp3Reader.ReadByte
Next
mp3Reader.Close()
End If
As you can see with this code we save all 128 byte of file to buffer. Next step is check if buffer contain ID3 data. if first 3 bytes of buffer was "TAG" it means the file have ID3v1 and if not it means file don't have ID3v1. we check it with following 'if':
If Encoding.Default.GetString(buffer, 0, 3).Equals("TAG") Then
'Read Data
End If
How to read data ? theres some kind of standard that you must know for reading this data. let me say how read data from our buffer. the bytes 0 to 2 as you saw must be equal to "TAG".
With this information we read ID3v1 data with following codes:
_Title = Encoding.Default.GetString(buffer, 3, 30)
_Artist = Encoding.Default.GetString(buffer, 33, 30)
_Album = Encoding.Default.GetString(buffer, 63, 30)
_Year = Encoding.Default.GetString(buffer, 93, 4)
_Comment = Encoding.Default.GetString(buffer, 97, 28)
_TitleNumber = Convert.ToInt32(buffer(126).ToString())
If Convert.ToInt32(buffer(127)) > 0 Then
_Genre = Convert.ToInt16(buffer(127))
End If
If file includes ID3 we set _HasTag and _HadTag to true else we set them false. we need to know if file Had Tag it need for write ID3 as you will see.
We have some public properties for Title, Artist, Album, Year, Comment, TrackNumber and Genre.
In main application we must read information with this properties and set them if we need and then call WriteID3
For write ID3 Data first we must fill our buffer with variables data. to do this run this code:
buffer.Clear(buffer, 0, 128)
Encoding.Default.GetBytes("TAG".ToUpper.ToCharArray()).CopyTo(buffer, 0)
Encoding.Default.GetBytes(_Title.ToCharArray()).CopyTo(buffer, 3)
Encoding.Default.GetBytes(_Artist.ToCharArray()).CopyTo(buffer, 33)
Encoding.Default.GetBytes(_Album.ToCharArray()).CopyTo(buffer, 63)
Encoding.Default.GetBytes(_Year.ToCharArray()).CopyTo(buffer, 93)
Encoding.Default.GetBytes(_Comment.ToCharArray()).CopyTo(buffer, 97)
buffer(126) = _TitleNumber
buffer(127) = Convert.ToInt32(_Genre)
As you see we cleared the buffer and then fill the buffer with variables data. As we said before first three bytes must be "TAG". remember "TAG" is case sensitive you can't use "Tag" or "TaG" or any thing except "TAG".
We need to open file for write data
Dim mp3Writer As FileStream = mp3File.OpenWrite()
If the file has tag now and had it before it means we must start overwrite data to final 128 bytes. and if file hadn't tag but now it have. it means we must write data at the end of file. or file had tag before and don't have tag now it means we must delete 128 bytes from end.
If _HasTag And _HadTag Then
mp3Writer.Seek(-128, SeekOrigin.End)
mp3Writer.Write(buffer, 0, 128)
ElseIf (Not _HadTag) And _HasTag Then
mp3Writer.Seek(0, SeekOrigin.End)
mp3Writer.Write(buffer, 0, 128)
_HadTag = True
ElseIf _HadTag And (Not _HasTag) Then
_HadTag = False
mp3Writer.SetLength(mp3Writer.Length - 128)
End If
mp3Writer.Close()
For those that more relax with C# visit http://www.codeproject.com/cs/media/id3v1editor.asp
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 18 Jan 2006 Editor: Chris Maunder |
Copyright 2006 by Hamed_ji Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |