PDA

View Full Version : Here's a script to read ID3v1 tags



martin
17-08-2009, 15:57
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 :oops: It would be great if somebody can solve this


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

Michael Clease
17-08-2009, 17:26
Try this



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"

martin
17-08-2009, 18:04
Hi Michael!

Thanks for your revised code. I googled a little bit and I believe that this UDT must be correct:

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

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

Kind regards,

Martin

Michael Clease
18-08-2009, 08:12
Martin is your mp3 a version 1 or higher

I removed this because it was rubbish.

martin
18-08-2009, 08:34
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

Petr Schreiber
18-08-2009, 09:24
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 :lol: (WinAmp says "Alt.Rock")
Is the list of genres really valid?


Petr

martin
18-08-2009, 09:39
Thanks for explanation Petr!

In the meantime I found the problem:

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

Revised code:

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,128)

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

Petr Schreiber
18-08-2009, 09:42
It works now! :eusaclap:

martin
04-09-2009, 19:03
In addition to above example, here a small code to get the bitrate of a mp3 file:


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

Petr Schreiber
05-09-2009, 19:50
Thanks Martin,

I will check this code out :)