Hi
Did anyone ever tried to read the ID3v1 and/or ID3v2-tag of MP3 files? If so, could you please post some code for it?
Thanx!
Printable View
Hi
Did anyone ever tried to read the ID3v1 and/or ID3v2-tag of MP3 files? If so, could you please post some code for it?
Thanx!
I Found this Class When i Was Cleaning My Visual Basic HardDrive That Should Do What You Want. But I have No Idea If I wrote it or If I got it From Some One :)!
This Belongs In A Class Module!
Private mvarFilename As String
Private mvarArtist As String
Private mvarTitle As String
Private mvarAlbum As String
Private mvarComment As String
Private mvarGenre As Integer
Private mvarYear As String
Private tagData As String * 128
Public isActive As Boolean
Public Property Let Genre(ByVal vData As Integer)
If (Len(Dir(mvarFilename))) = 0 Then Exit Property
If vData < 0 Or vData > 255 Then vData = 255
Mid(tagData, 128, 1) = Chr(vData)
mvarGenre = vData
End Property
Public Property Get Genre() As Integer
If (Len(Dir(mvarFilename))) = 0 Then Exit Property
Genre = mvarGenre
End Property
Public Property Let Comment(ByVal vData As String)
If (Len(Dir(mvarFilename))) = 0 Then Exit Property
If Len(vData) > 30 Then vData = Left(vData, 30) 'If too big, trim to 30
If Len(vData) < 30 Then vData = vData & String(30 - Len(vData), Chr(0)) 'If too small, fill the rest with nulls
Mid(tagData, 98, 30) = vData
mvarComment = vData
End Property
Public Property Get Comment() As String
If (Len(Dir(mvarFilename))) = 0 Then Exit Property
Comment = mvarComment
End Property
Public Property Let Album(ByVal vData As String)
If (Len(Dir(mvarFilename))) = 0 Then Exit Property
If Len(vData) > 30 Then vData = Left(vData, 30) 'If too big, trim to 30
If Len(vData) < 30 Then vData = vData & String(30 - Len(vData), Chr(0)) 'If too small, fill the rest with nulls
Mid(tagData, 64, 30) = vData
mvarAlbum = vData
End Property
Public Property Get Album() As String
If (Len(Dir(mvarFilename))) = 0 Then Exit Property
Album = mvarAlbum
End Property
Public Property Let Title(ByVal vData As String)
If (Len(Dir(mvarFilename))) = 0 Then Exit Property
If Len(vData) > 30 Then vData = Left(vData, 30) 'If too big, trim to 30
If Len(vData) < 30 Then vData = vData & String(30 - Len(vData), Chr(0)) 'If too small, fill the rest with nulls
Mid(tagData, 4, 30) = vData
mvarTitle = vData
End Property
Public Property Get Title() As String
If (Len(Dir(mvarFilename))) = 0 Then Exit Property
Title = mvarTitle
End Property
Public Property Let Artist(ByVal vData As String)
If (Len(Dir(mvarFilename))) = 0 Then Exit Property
If Len(vData) > 30 Then vData = Left(vData, 30) 'If too big, trim to 30
If Len(vData) < 30 Then vData = vData & String(30 - Len(vData), Chr(0)) 'If too small, fill the rest with nulls
Mid(tagData, 34, 30) = vData
mvarArtist = vData
End Property
Public Property Get Artist() As String
If (Len(Dir(mvarFilename))) = 0 Then Exit Property
Artist = mvarArtist
End Property
Public Property Let Filename(ByVal vData As String)
Dim fileNum As Long, filePos As Long
If Len(Dir(vData)) = 0 Then
GoTo WRONG
Else
If UCase(Right(vData, 4)) <> ".MP3" Then GoTo WRONG
fileNum = FreeFile
filePos = FileLen(vData) - 127
If filePos > 0 Then
Open vData For Binary As #fileNum
Get #fileNum, filePos, tagData
Close #fileNum
If Left(tagData, 3) <> "TAG" Then GoTo WRONG
mvarTitle = Replace(Trim(Mid(tagData, 4, 30)), Chr(0), "")
mvarArtist = Replace(Trim(Mid(tagData, 34, 30)), Chr(0), "")
mvarAlbum = Replace(Trim(Mid(tagData, 64, 30)), Chr(0), "")
mvarYear = Replace(Trim(Mid(tagData, 94, 4)), Chr(0), "")
mvarComment = Replace(Trim(Mid(tagData, 98, 30)), Chr(0), "")
mvarGenre = Asc(Mid(tagData, 128, 1))
Else
GoTo WRONG
End If
End If
isActive = True
mvarFilename = vData
Exit Property
WRONG: ' Jumps here if bad file
isActive = False
mvarFilename = ""
End Property
Public Property Get Filename() As String
Filename = mvarFilename
End Property
Public Property Let Year(ByVal vData As String)
If (Len(Dir(mvarFilename))) = 0 Then Exit Property
If Len(vData) > 4 Then vData = Left(vData, 4) 'If too big, trim to 30
If Len(vData) < 4 Then vData = vData & String(4 - Len(vData), Chr(0)) 'If too small, fill the rest with nulls
Mid(tagData, 94, 4) = vData
mvarYear = vData
End Property
Public Property Get Year() As String
If (Len(Dir(mvarFilename))) = 0 Then Exit Property
Year = mvarYear
End Property
Public Function SaveTag() As String
Dim errDesc As String ' Error description
Dim fileNum As Long, filePos As Long
Dim fileData As String
If Len(Dir(mvarFilename)) = 0 Then
errDesc = "File not found"
GoTo WRONG
Else
If UCase(Right(mvarFilename, 4)) <> ".MP3" Then errDesc = "Wrong file extension. Must be .mp3": GoTo WRONG
fileNum = FreeFile
fileData = String(FileLen(mvarFilename), Chr(0)) 'Buffer
If FileLen(mvarFilename) > 128 Then
Open mvarFilename For Binary As #fileNum
Get #fileNum, 1, fileData
Close #fileNum
If Left(Right(fileData, 128), 3) <> "TAG" Then GoTo WRONG
Mid(fileData, Len(fileData) - 127) = tagData
fileNum = FreeFile
Open mvarFilename For Binary As #fileNum
Put #fileNum, 1, fileData
Close #fileNum
Else
GoTo WRONG
End If
End If
Exit Function
WRONG: 'Go here if error writing to file
isActive = False
mvarFilename = ""
SaveTag = errDesc
End Function
Whao! That's a helluva lot of code. But thenks very much I'll use it defenately!!
But, I reduced it to this:
VB Code:
Private Sub Form_Load() Dim tagData As String * 128 vData = "D:\Alex\Mp3\Muziek\Cornershop - Brimful of Asha.mp3" If Len(Dir(vData)) = 0 Then MsgBox "Error, file not found": Exit Sub Else If UCase(Right(vData, 4)) <> ".MP3" Then MsgBox "Error, not an Mp3 file": Exit Sub fileNum = FreeFile filePos = FileLen(vData) - 127 If filePos > 0 Then Open vData For Binary As #fileNum Get #fileNum, filePos, tagData Close #fileNum If Left(tagData, 3) <> "TAG" Then MsgBox "Error, tag not located": Exit Sub mvarTitle = Replace(Trim(Mid(tagData, 4, 30)), Chr(0), "") mvarArtist = Replace(Trim(Mid(tagData, 34, 30)), Chr(0), "") mvarAlbum = Replace(Trim(Mid(tagData, 64, 30)), Chr(0), "") mvarYear = Replace(Trim(Mid(tagData, 94, 4)), Chr(0), "") mvarComment = Replace(Trim(Mid(tagData, 98, 30)), Chr(0), "") mvarGenre = Asc(Mid(tagData, 128, 1)) Else MsgBox "Error, file too short to be tagged Mp3-file": Exit Sub End If End If Text1 = "Title " & mvarTitle & vbCrLf Text1 = Text1 & "Artist " & mvarArtist & vbCrLf Text1 = Text1 & "Album " & mvarAlbum & vbCrLf Text1 = Text1 & "Year " & mvarYear & vbCrLf Text1 = Text1 & "Comment " & mvarComment & vbCrLf Text1 = Text1 & "Genre " & mvarGenre End Sub