Library code snippets

MP3 Tag information

The Windows Media Player provides an easy, quick way to drop MP3
capability into a Visual Basic application. However, once you have
an MP3, you may have wondered how to read information about the song,
such as the song title and artist's name. If the MP3 file uses the
most popular tag encryption, ID3, then you're in luck. This standard
stores the Tag information in the last 128 bytes of the file
(Tag:3, Title:30, Artist:30, Album:30, Year:4, Comment:30, Genre:1)

To read this information, first open the MP3 file and grab the last
128 bytes. With ID3, the first three slots hold the string TAG if the
file actually contains information. If the file does contain Tag
information, store the last 128 bytes in a custom variable. After that,
cycle through the MP3 file, extracting information as you go. The
following procedure shows the code that extracts this information as
well as creates several important variables to use later on:

Option Explicit
Private Type TagInfo
    Tag As String * 3
    Songname As String * 30
    artist As String * 30
    album As String * 30
    year As String * 4
    comment As String * 30
    genre As String * 1
End Type

Dim FileName As String
Dim CurrentTag As TagInfo

Private Sub Form1_Load()
Dim temp As String
On Error Resume Next

FileName = App.Path & "myMP3.mp3"   
Open FileName For Binary As #1
With CurrentTag
    Get #1, FileLen(FileName) - 127, .Tag
    If Not .Tag = "TAG" Then
        label8.Caption = "No tag"
        Close #1
        Exit Sub
    End If
    Get #1, , .Songname
    Get #1, , .artist
    Get #1, , .album
    Get #1, , .year
    Get #1, , .comment
    Get #1, , .genre
    Close #1

    txtTitle = RTrim(.Songname)
    txtArtist = RTrim(.artist)
    txtAlbum = RTrim(.album)
    txtYear = RTrim(.year)
    txtComment = RTrim(.comment)

    Temp = RTrim(.genre)
    txtGenreCode = Asc(Temp)
    Combo1.ListIndex = CInt(txtGenreCode) - 1
End With
End Sub


Notice that the code has to handle the genre character a little
differently. That's because ID3 stores this data as a single ASCII
character. To match up the actual number with its corresponding
description--say contained in a combobox--the procedure converts the
ASCII to a number, and then looks up that number in the combobox.

Comments

  1. 06 Jun 2008 at 21:44

    Why is Tag included in the TYPE definition? It would appear to me that once Tag = "TAG" then you could use one GET to gather the other information using the Type variable DIMensioned. Interesting ideas here!

    Thanks,

    Ted

  2. 14 Mar 2004 at 13:44

    Here you can find something


    http://members.a1.net/pasce/index.htm

  3. 14 Mar 2004 at 13:41

    hi!


    http://members.a1.net/pasce/index.htm


    here you find something to read & write Id3v1/Id3v2 Tags!


    mfg VBCool



  4. 09 Dec 2003 at 11:21

    Thanks thats an excellent article and works well...


    I would like two supplementals:
    The first on how to read mp3 quality information  192kb/s  etc
    The second on how to write this data back out to the file.


    It would be real handy also if there was a list of the combo list of genre relative to the asc code
    (Would of got 5 on my vote if it had the combo code)

  5. 13 Jun 2003 at 08:01

    Quote:
    [1]Posted by rama_ii on 9 Sep 2002 07:08 PM[/1]
    I had the same problem with id3v2 tag. Maybe this URL will help you:
    http://id3lib.sourceforge.net/

    Did you get it to work? The documentation is only for C++ and I want to use it in VB... Does anybody know how to do that?

  6. 13 Jun 2003 at 08:01

    Quote:
    [1]Posted by rama_ii on 9 Sep 2002 07:08 PM[/1]
    I had the same problem with id3v2 tag. Maybe this URL will help you:
    http://id3lib.sourceforge.net/

    Did you get it to work? The documentation is only for C++ and I want to use it in VB... Does anybody know how to do that?

  7. 13 Jun 2003 at 08:00

    Quote:
    [1]Posted by rama_ii on 9 Sep 2002 07:08 PM[/1]
    I had the same problem with id3v2 tag. Maybe this URL will help you:
    http://id3lib.sourceforge.net/

    Did you get it to work? The documentation is only for C++ and I want to use it in VB... Does anybody know how to do that?

  8. 12 Mar 2003 at 22:12
    I'm afraid that I don't have anything in c# for you, but some advice on whether to load the info first or not.  My suggestion would be to load the info first.  This is because if the user wants to do several searches, preloading all the mp3s will mean that you only have to access the file info once instead of several times.  This will save vast amounts of time for people like me who keep a BIG number of mp3s

    Hope this is of some help  

    Oh, and don't use the "Back" button on your browser to edit a post, use the "Edit" button.
    You might want to delete some of those extra posts of yours
  9. 12 Mar 2003 at 22:09
    Does anyone have similar code for C# to read ID3V2 tags.

    Also, If wanted to write an application to search ID3V2 tags and return results, is it better to first load all media into some database, or search the files individually?

    adam2307@hotmail.com

    regards
  10. 12 Mar 2003 at 22:07
    Does anyone have similar code for C# to read ID3V2 tags.

    Also, If wanted to write an application to search ID3V2 tags and return results, is it better to first load all media into some database, or search the files individually?

    adam2307@hotmail.com

    regards
  11. 12 Mar 2003 at 22:06

    Does anyone have similar code for C# to read ID3V2 tags.


    Also, If wanted to write an application to search ID3V2 tags and return results, is it better to first load all media into some database, or search the files individually?


    adam2307@hotmail.com


    regards

  12. 12 Mar 2003 at 21:51

    Does anyone have similar code for C# to read ID3V2 tags.


    Also, If wanted to write an application to search ID3V2 tags and return results, is it better to first load all media into some database, or search the files individually?


    adam2307@hotmail.com


    regards

  13. 22 Jan 2003 at 12:53

    This is really sweet, but I would also like to know how to SET MP3 tag information


    Thanks!

  14. 09 Sep 2002 at 19:08

    I had the same problem with id3v2 tag. Maybe this URL will help you:
    http://id3lib.sourceforge.net/


  15. 16 Jul 2002 at 12:31

    I have written an application (mp3-portal-intranet) thing once,
    and I recognize the problem, but maybe my code can help you.

  16. 15 Jun 2002 at 13:25

    You're right that far I think.  I have found some vb code that can read and write v2 since I posted this.  It crashes sometimes when certain fields are empty though; and it will only read the basic v2 fields.  I've been trying to fix it.  Its kinda long, if you email me I'll send you the zip it came in.  

  17. 15 Jun 2002 at 12:52

    I was wondering the same thing as you and I decided that they probably wouldn’t put both V1 and V2 at the same end so I took a look at the beginning and I did get something but not what I hoped for.  


    It appears at least that the ID3V2 info is held at the beginning, but it only appears to hold the strings which have been filled in, IE: if whoever entered the info included the song title but not the artist then it WILL NOT store a blank string for the artist.


    I could be completely wrong about all of this so I'll post another message when I have found out how to decode it fully, but if someone beats me to it then I’d appreciate a reply to this message to save me wasting my time…

  18. 11 Jun 2002 at 19:30

    This seems pretty straightforward.  However what about ID3v2 tags?  I know MP3's can have v1, v2, or both.  How can you read v2 using VB?


    Thanks.

  19. 01 Jan 1999 at 00:00

    This thread is for discussions of MP3 Tag information.

Leave a comment

Sign in or Join us (it's free).

ElementK Journals

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!