Results 1 to 10 of 10

Thread: Here's a script to read ID3v1 tags

  1. #1
    Member
    Join Date
    Mar 2009
    Location
    Netherlands
    Age
    52
    Posts
    248
    Rep Power
    40

    Here's a script to read ID3v1 tags

    Ciao everybody,

    I just made a script that can read the ID3v1 tags from a MP3 file. Please grab it if you can use it :-)

    The only problem I have is reading the genre. It's a BYTE but I don't know how I can convert a byte to a number It would be great if somebody can solve this

    [code=thinbasic]uses "file"
    uses "console"

    dim g as string = "Blues,Classic Rock,Country,Dance,Disco,Funk,Grunge,Hip-Hop,Jazz,Metal,New Age,Oldies,Other,Pop,R&B,Rap,Reggae,Rock,Techno,Industrial,Alternative,Ska,Death Metal,Pranks,Soundtrack,Euro-Techno,Ambient,Trip-Hop,Vocal,Jazz+Funk,Fusion,Trance,Classical,Instrumental,Acid,House,Game,Sound Clip,Gospel,Noise,AlternRock,Bass,Soul,Punk,Space,Meditative,Instrumental Pop,Instrumental Rock,Ethnic,Gothic,Darkwave,Techno-Industrial,Electronic,Pop-Folk,Eurodance,Dream,Southern Rock,Comedy,Cult,Gangsta,Top 40,Christian Rap,Pop/Funk,Jungle,Native American,Cabaret,New Wave,Psychadelic,Rave,Showtunes,Trailer,Lo-Fi,Tribal,Acid Punk,Acid Jazz,Polka,Retro,Musical,Rock & Roll,Hard Rock,Folk,Folk-Rock,National Folk,Swing,Fast Fusion,Bebob,Latin,Revival,Celtic,Bluegrass,Avantgarde,Gothic Rock,Progressive Rock,Psychedelic Rock,Symphonic Rock,Slow Rock,Big Band,Chorus,Easy Listening,Acoustic,Humour,Speech,Chanson,Opera,Chamber Music,Sonata,Symphony,Booty Bass,Primus,Porn Groove,Satire,Slow Jam,Club,Tango,Samba,Folklore,Ballad,Power Ballad,Rhythmic Soul,Freestyle,Duet,Punk Rock,Drum Solo,A capella,Euro-House,Dance Hall"

    dim style(126) as string
    split(g,",",style)

    Type Tag
    Header As String * 3
    SongTitle As String * 30
    Artist As String * 30
    Album As String * 30
    Year As String * 4
    Comment As String * 30
    Genre As Byte
    End Type

    Dim ID3Tag as Tag
    dim filename as string = "YourMp3File.mp3"
    Dim FileNum As Long = FILE_OPEN(FileName,"Binary")

    file_seek(filenum,file_lof(filenum)-127)
    ID3Tag.header=file_Get(FileNum, 3)

    id3tag.songtitle=file_Get(filenum,30)
    id3tag.artist=file_Get(filenum,30)
    id3tag.album=file_Get(filenum,30)
    id3tag.year=file_Get(filenum,4)
    id3tag.comment=file_Get(filenum,30)
    id3tag.genre=file_Get(filenum,1)

    file_Close(FileNum)

    if ID3Tag.header="TAG" then
    printl "HEADER " & ID3Tag.header
    printl "Title " & trim$(ID3tag.songtitle)
    printl "Artist " & trim$(ID3tag.artist)
    printl "Album " & trim$(ID3tag.album)
    printl "Year " & trim$(ID3tag.year)
    'printl "Genre " & style(cvbyt(id3tag.genre)+1)
    end if

    printl "press key to stop"
    waitkey[/code]



  2. #2
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    159

    Re: Here's a script to read ID3v1 tags

    Try this

    [code=thinbasic]
    uses "file"
    uses "console"

    dim g as string = "Blues,Classic Rock,Country,Dance,Disco,Funk,Grunge,Hip-Hop,Jazz,Metal,New Age,Oldies,Other,Pop,R&B,Rap,Reggae,Rock,Techno,Industrial,Alternative,Ska,Death Metal,Pranks,Soundtrack,Euro-Techno,Ambient,Trip-Hop,Vocal,Jazz+Funk,Fusion,Trance,Classical,Instrumental,Acid,House,Game,Sound Clip,Gospel,Noise,AlternRock,Bass,Soul,Punk,Space,Meditative,Instrumental Pop,Instrumental Rock,Ethnic,Gothic,Darkwave,Techno-Industrial,Electronic,Pop-Folk,Eurodance,Dream,Southern Rock,Comedy,Cult,Gangsta,Top 40,Christian Rap,Pop/Funk,Jungle,Native American,Cabaret,New Wave,Psychadelic,Rave,Showtunes,Trailer,Lo-Fi,Tribal,Acid Punk,Acid Jazz,Polka,Retro,Musical,Rock & Roll,Hard Rock,Folk,Folk-Rock,National Folk,Swing,Fast Fusion,Bebob,Latin,Revival,Celtic,Bluegrass,Avantgarde,Gothic Rock,Progressive Rock,Psychedelic Rock,Symphonic Rock,Slow Rock,Big Band,Chorus,Easy Listening,Acoustic,Humour,Speech,Chanson,Opera,Chamber Music,Sonata,Symphony,Booty Bass,Primus,Porn Groove,Satire,Slow Jam,Club,Tango,Samba,Folklore,Ballad,Power Ballad,Rhythmic Soul,Freestyle,Duet,Punk Rock,Drum Solo,A capella,Euro-House,Dance Hall"

    dim style() as string
    split(g,",",style)

    Type Tag
    Header As String * 3
    SongTitle As String * 30
    Artist As String * 30
    Album As String * 30
    Year As String * 4
    Comment As String * 28
    Track As Byte
    Genre As Byte
    End Type

    Dim ID3Tag as Tag
    dim filename as string = "aerosmith.mp3"
    Dim FileNum As Long = FILE_OPEN(FileName,"Binary")

    IF Filenum = 0 THEN MSGBOX 0," No File Found" : STOP

    file_seek(filenum,file_lof(filenum)-127)

    ID3Tag = FILE_GET(filenum,127)

    file_Close(FileNum)

    if ID3Tag.header="TAG" then
    printl "HEADER : " & ID3Tag.header
    printl "Title : " & trim$(ID3tag.songtitle)
    printl "Artist : " & trim$(ID3tag.artist)
    printl "Album : " & trim$(ID3tag.album)
    printl "Year : " & trim$(ID3tag.year)
    printl "Genre : " & style((id3tag.genre)+1)
    end if

    printl "press key to stop"

    [/code]
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  3. #3
    Member
    Join Date
    Mar 2009
    Location
    Netherlands
    Age
    52
    Posts
    248
    Rep Power
    40

    Re: Here's a script to read ID3v1 tags

    Hi Michael!

    Thanks for your revised code. I googled a little bit and I believe that this UDT must be correct:
    [code=thinbasic]Type Tag
    Header As String * 3
    SongTitle As String * 30
    Artist As String * 30
    Album As String * 30
    Year As String * 4
    Comment As String * 28
    Dummy as byte
    Track As Byte
    Genre As Byte
    End Type[/code]

    But still I retrieve 0 for Genre, must do some more research to find out how to get this genre index

    Kind regards,

    Martin

  4. #4
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    159

    Re: Here's a script to read ID3v1 tags

    Martin is your mp3 a version 1 or higher

    I removed this because it was rubbish.
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  5. #5
    Member
    Join Date
    Mar 2009
    Location
    Netherlands
    Age
    52
    Posts
    248
    Rep Power
    40

    Re: Here's a script to read ID3v1 tags

    Hi Michael,

    Most of my mp3's are id3V2. But with your script I still get the wrong Genre index number (did you get a good result?).

    I saw an Visual Basic example, where the command CSTR was used to convert the Genre Byte into a number. http://msdn.microsoft.com/en-us/library/ch47ss2a(VS.80).aspx Unfortunately I can't find a simulair command in ThinBasic.

    Have a nice day,

    Martin

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Here's a script to read ID3v1 tags

    Hi Martin,

    BYTE already is a number, in range 0..255. To display it in string you can use STR$, TSTR$ or FORMAT$.
    I think the problem will lay somewhere else, but I do not have much experience with MP3 ID Tags.
    Some of my MP3 files begin with string "ID3", but as I have googled, there are already at least 4 versions of the header.

    With latest Michaels code, I also get quite confusing genre rating - like Hip Hop for Oasis (WinAmp says "Alt.Rock")
    Is the list of genres really valid?


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  7. #7
    Member
    Join Date
    Mar 2009
    Location
    Netherlands
    Age
    52
    Posts
    248
    Rep Power
    40

    Re: Here's a script to read ID3v1 tags

    Thanks for explanation Petr!

    In the meantime I found the problem:

    ID3Tag = FILE_GET(filenum,127) ----> value must be 128 !

    Revised code:
    [code=thinbasic]uses "file",uses "console"

    dim style as string = ",Blues,Classic Rock,Country,Dance,Disco,Funk,Grunge,Hip-Hop,Jazz,Metal,New Age,Oldies,Other,Pop,R&B,Rap,Reggae,Rock,Techno,Industrial,Alternative,Ska,Death Metal,Pranks,Soundtrack,Euro-Techno,Ambient,Trip-Hop,Vocal,Jazz+Funk,Fusion,Trance,Classical,Instrumental,Acid,House,Game,Sound Clip,Gospel,Noise,AlternRock,Bass,Soul,Punk,Space,Meditative,Instrumental Pop,Instrumental Rock,Ethnic,Gothic,Darkwave,Techno-Industrial,Electronic,Pop-Folk,Eurodance,Dream,Southern Rock,Comedy,Cult,Gangsta,Top 40,Christian Rap,Pop/Funk,Jungle,Native American,Cabaret,New Wave,Psychadelic,Rave,Showtunes,Trailer,Lo-Fi,Tribal,Acid Punk,Acid Jazz,Polka,Retro,Musical,Rock & Roll,Hard Rock,Folk,Folk-Rock,National Folk,Swing,Fast Fusion,Bebob,Latin,Revival,Celtic,Bluegrass,Avantgarde,Gothic Rock,Progressive Rock,Psychedelic Rock,Symphonic Rock,Slow Rock,Big Band,Chorus,Easy Listening,Acoustic,Humour,Speech,Chanson,Opera,Chamber Music,Sonata,Symphony,Booty Bass,Primus,Porn Groove,Satire,Slow Jam,Club,Tango,Samba,Folklore,Ballad,Power Ballad,Rhythmic Soul,Freestyle,Duet,Punk Rock,Drum Solo,A capella,Euro-House,Dance Hall,"

    Type Tag
    Header As String * 3
    SongTitle As String * 30
    Artist As String * 30
    Album As String * 30
    Year As String * 4
    Comment As String * 28
    Dummy as byte
    Track as byte
    Genre As Byte
    End Type

    Dim ID3Tag as Tag
    dim filename as string = "YourMP3file.mp3"
    Dim FileNum As Long = FILE_OPEN(FileName,"Binary")

    IF Filenum = 0 THEN MSGBOX 0," No File Found"

    file_seek(filenum,file_lof(filenum)-127)
    ID3Tag = FILE_GET(filenum,12

    file_Close(FileNum)

    if ID3Tag.header="TAG" then
    printl "Header : " & ID3Tag.header
    printl "Track : " & ID3Tag.track
    printl "Title : " & trim$(ID3tag.songtitle)
    printl "Artist : " & trim$(ID3tag.artist)
    printl "Album : " & trim$(ID3tag.album)
    printl "Year : " & trim$(ID3tag.year)
    printl "Comment : " & trim$(id3tag.comment)
    printl "Genre : " & grab$(style,",",",",id3tag.genre+1)
    end if

    printl "press key to stop"
    waitkey
    [/code]

  8. #8
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Here's a script to read ID3v1 tags

    It works now!
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  9. #9
    Member
    Join Date
    Mar 2009
    Location
    Netherlands
    Age
    52
    Posts
    248
    Rep Power
    40

    Re: Here's a script to read ID3v1 tags

    In addition to above example, here a small code to get the bitrate of a mp3 file:

    [code=thinbasic]uses "file","tbass"
    dim filename as string = "YourFileHere.mp3"
    dim channel as long = TBASS_STREAMCREATEFILE(%tbass_FALSE, filename, 0, 0, %TBASS_STREAM_AUTOFREE)
    dim bitrate as long = Int((file_size(filename) / (125 * tBASS_ChannelBytes2Seconds(channel, tBASS_ChannelGetLength(channel)))) + 0.5)
    msgbox 0,"Bitrate of " & filename & " is: " & bitrate[/code]

  10. #10
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Here's a script to read ID3v1 tags

    Thanks Martin,

    I will check this code out
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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