Results 1 to 4 of 4

Thread: MP3: ID3v1/ID3v2-tag

  1. #1

    Thread Starter
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    MP3: ID3v1/ID3v2-tag

    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!
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  2. #2
    Hyperactive Member ZeroCool's Avatar
    Join Date
    Feb 2002
    Location
    In front of my computer
    Posts
    423

    Wink Class Code!

    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

  3. #3

    Thread Starter
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719
    Whao! That's a helluva lot of code. But thenks very much I'll use it defenately!!
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  4. #4

    Thread Starter
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719
    But, I reduced it to this:

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim tagData As String * 128
    3.     vData = "D:\Alex\Mp3\Muziek\Cornershop - Brimful of Asha.mp3"
    4.     If Len(Dir(vData)) = 0 Then
    5.          MsgBox "Error, file not found": Exit Sub
    6.     Else
    7.         If UCase(Right(vData, 4)) <> ".MP3" Then MsgBox "Error, not an Mp3 file": Exit Sub
    8.         fileNum = FreeFile
    9.         filePos = FileLen(vData) - 127
    10.         If filePos > 0 Then
    11.             Open vData For Binary As #fileNum
    12.                 Get #fileNum, filePos, tagData
    13.             Close #fileNum
    14.             If Left(tagData, 3) <> "TAG" Then MsgBox "Error, tag not located": Exit Sub
    15.             mvarTitle = Replace(Trim(Mid(tagData, 4, 30)), Chr(0), "")
    16.             mvarArtist = Replace(Trim(Mid(tagData, 34, 30)), Chr(0), "")
    17.             mvarAlbum = Replace(Trim(Mid(tagData, 64, 30)), Chr(0), "")
    18.             mvarYear = Replace(Trim(Mid(tagData, 94, 4)), Chr(0), "")
    19.             mvarComment = Replace(Trim(Mid(tagData, 98, 30)), Chr(0), "")
    20.             mvarGenre = Asc(Mid(tagData, 128, 1))
    21.         Else
    22.             MsgBox "Error, file too short to be tagged Mp3-file": Exit Sub
    23.         End If
    24.     End If
    25.     Text1 = "Title    " & mvarTitle & vbCrLf
    26.     Text1 = Text1 & "Artist   " & mvarArtist & vbCrLf
    27.     Text1 = Text1 & "Album    " & mvarAlbum & vbCrLf
    28.     Text1 = Text1 & "Year     " & mvarYear & vbCrLf
    29.     Text1 = Text1 & "Comment  " & mvarComment & vbCrLf
    30.     Text1 = Text1 & "Genre    " & mvarGenre
    31. End Sub
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width