[QUIZ][SOLUTION] ID3 Tags (#136)

J

James Koppel

$genres =3D ["Blues","Classic Rock","Country","Dance","Disco","Funk",=0A "=
Grunge","Hip-Hop","Jazz","Metal","New Age","Oldies",=0A "Other","Pop","R&B=
","Rap","Reggae","Rock","Techno",=0A "Industrial","Alternative","Ska","Dea=
th Metal","Pranks",=0A "Soundtrack","Euro-Techno","Ambient","Trip-Hop","Vo=
cal",=0A "Jazz+Funk","Fusion","Trance","Classical","Instrumental",=0A "Ac=
id","House","Game","Sound Clip","Gospel","Noise",=0A "AlternRock","Bass","=
Soul","Punk","Space","Meditative",=0A "Instrumental Pop","Instrumental Roc=
k","Ethnic","Gothic",=0A "Darkwave","Techno-Industrial","Electronic","Pop-=
Folk",=0A "Eurodance","Dream","Southern Rock","Comedy","Cult","Gangsta",=
=0A "Top 40","Christian Rap","Pop/Funk","Jungle",=0A "Native American","C=
abaret","New Wave","Psychadelic","Rave",=0A "Showtunes","Trailer","Lo-Fi",=
"Tribal","Acid Punk",=0A "Acid Jazz","Polka","Retro","Musical","Rock & Rol=
l",=0A "Hard Rock","Folk","Folk-Rock","National Folk","Swing",=0A "Fast F=
usion","Bebob","Latin","Revival","Celtic","Bluegrass",=0A "Avantgarde","Go=
thic Rock","Progressive Rock",=0A "Psychedelic Rock","Symphonic Rock","Slo=
w Rock","Big Band",=0A "Chorus","Easy Listening","Acoustic","Humour","Spee=
ch",=0A "Chanson","Opera","Chamber Music","Sonata","Symphony",=0A "Booty =
Bass","Primus","Porn Groove","Satire","Slow Jam",=0A "Club","Tango","Samba=
","Folklore","Ballad","Power Ballad",=0A "Rhythmic Soul","Freestyle","Duet=
","Punk Rock","Drum Solo",=0A "A capella","Euro-House","Dance Hall"]=0A=0A=
=0Afile =3D File.new(ARGV[0].chomp)=0A=0Atag, song, artist, album, year, co=
mment, genre =3D=0A file.read[-128..-1].unpack("A3A30A30A30A4A30C")=0A=0Af=
ile.close=0A=0Atrack =3D nil=0A=0Agenre =3D $genres[genre.to_i]=0A=0Aif com=
ment[28] =3D=3D 0=0A track =3D comment[29]=0A comment =3D comment[0..27]=
=0Aend=0A=0Aif tag =3D=3D "TAG"=0A [:song, :artist, :album, :track, :year,=
:comment, :genre].each do |sym|=0A puts sym.to_s.capitalize + ": " + ev=
al(sym.id2name).to_s=0A end=0Aelse=0A puts "Did not find ID3v1 tags."=0Ae=
nd=0A=0A=0A----- Original Message ----=0AFrom: Ruby Quiz <james@grayproduct=
ions.net>=0ATo: ruby-talk ML <[email protected]>=0ASent: Friday, Augu=
st 24, 2007 7:34:47 AM=0ASubject: [QUIZ] ID3 Tags (#136)=0A=0AThe three rul=
es of Ruby Quiz:=0A=0A1. Please do not post any solutions or spoiler discu=
ssion for this quiz until=0A48 hours have passed from the time on this mess=
age.=0A=0A2. Support Ruby Quiz by submitting ideas as often as you can:=0A=
=0Ahttp://www.rubyquiz.com/=0A=0A3. Enjoy!=0A=0ASuggestion: A [QUIZ] in t=
he subject of emails about the problem helps everyone=0Aon Ruby Talk follow=
the discussion. Please reply to the original quiz message,=0Aif you can.=
=0A=0A-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=
=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D=
-=3D-=3D-=3D-=3D=0A=0AThe MP3 file format, didn't provide any means for inc=
luding metadata about the=0Asong. ID3 tags were invented to solve this pro=
blem.=0A=0AYou can tell if an MP3 file includes ID3 tags by examining the l=
ast 128 bytes of=0Athe file. If they begin with the characters TAG, you ha=
ve found an ID3 tag. =0AThe format of the tag is as follows:=0A=0A TAG s=
ong album artist comment year genre=0A=0AThe spaces above are just for us h=
umans. The actual tags are fixed-width fields=0Awith no spacing between th=
em. Song, album, artist, and comment are 30 bytes=0Aeach. The year is fou=
r bytes and the genre just gets one, which is an index=0Ainto a list of pre=
defined genres I'll include at the end of this quiz.=0A=0AA minor change wa=
s later made to ID3 tags to allow them to include track=0Anumbers, creating=
ID3v1.1. In that format, if the 29th byte of a comment is=0Anull and the =
30th is not, the 30th byte is an integer representing the track=0Anumber.=
=0A=0ALater changes evolved ID3v2 which is a scary beast we won't worry abo=
ut.=0A=0AThis week's Ruby Quiz is to write an ID3 tag parser. Using a libr=
ary is=0Acheating. Roll up your sleeves and parse it yourself. It's not h=
ard at all.=0A=0AIf you don't have MP3 files to test your solution on, you =
can find some free=0Afiles at:=0A=0A http://www.mfiles.co.uk/mp3-files.h=
tm=0A=0AHere's the official genre list with some extensions added by Winamp=
:=0A=0A Blues=0A Classic Rock=0A Country=0A Dance=0A Disco=
=0A Funk=0A Grunge=0A Hip-Hop=0A Jazz=0A Metal=0A New Age=
=0A Oldies=0A Other=0A Pop=0A R&B=0A Rap=0A Reggae=0A =
Rock=0A Techno=0A Industrial=0A Alternative=0A Ska=0A Death =
Metal=0A Pranks=0A Soundtrack=0A Euro-Techno=0A Ambient=0A T=
rip-Hop=0A Vocal=0A Jazz+Funk=0A Fusion=0A Trance=0A Classic=
al=0A Instrumental=0A Acid=0A House=0A Game=0A Sound Clip=0A=
Gospel=0A Noise=0A AlternRock=0A Bass=0A Soul=0A Punk=0A=
Space=0A Meditative=0A Instrumental Pop=0A Instrumental Rock=
=0A Ethnic=0A Gothic=0A Darkwave=0A Techno-Industrial=0A Ele=
ctronic=0A Pop-Folk=0A Eurodance=0A Dream=0A Southern Rock=0A =
Comedy=0A Cult=0A Gangsta=0A Top 40=0A Christian Rap=0A Po=
p/Funk=0A Jungle=0A Native American=0A Cabaret=0A New Wave=0A =
Psychadelic=0A Rave=0A Showtunes=0A Trailer=0A Lo-Fi=0A Tr=
ibal=0A Acid Punk=0A Acid Jazz=0A Polka=0A Retro=0A Musical=
=0A Rock & Roll=0A Hard Rock=0A Folk=0A Folk-Rock=0A Nationa=
l Folk=0A Swing=0A Fast Fusion=0A Bebob=0A Latin=0A Revival=
=0A Celtic=0A Bluegrass=0A Avantgarde=0A Gothic Rock=0A Prog=
ressive Rock=0A Psychedelic Rock=0A Symphonic Rock=0A Slow Rock=0A=
Big Band=0A Chorus=0A Easy Listening=0A Acoustic=0A Humour=
=0A Speech=0A Chanson=0A Opera=0A Chamber Music=0A Sonata=0A=
Symphony=0A Booty Bass=0A Primus=0A Porn Groove=0A Satire=
=0A Slow Jam=0A Club=0A Tango=0A Samba=0A Folklore=0A Bal=
lad=0A Power Ballad=0A Rhythmic Soul=0A Freestyle=0A Duet=0A =
Punk Rock=0A Drum Solo=0A A capella=0A Euro-House=0A Dance Hal=
l=0A=0A=0A=0A=0A=0A=0A =0A___________________________________________=
_________________________________________=0ASick sense of humor? Visit Yaho=
o! TV's =0AComedy with an Edge to see what's on, when. =0Ahttp://tv.yahoo.c=
om/collections/222
 
E

Eugene Kalenkovich

def fileTail (file, offset)
f=File.new(file)
f.seek(-offset,IO::SEEK_END)
f.read
end

class ID3Tag
GENRES=["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"]
attr_reader :title, :artist, :album, :year, :comment, :genre, :track
def initialize fname
tag,@title,@artist,@album,@year,@comment,@genre=fileTail(fname,128).unpack
"A3A30A30A30A4A30C"
raise "No ID3 Info" if tag!='TAG'
s_com,flag,[email protected] "A28CC"
if flag==0 and track!=0
@comment=s_com
@track=track
end
@genre=GENRES[@genre]
@genre="Unknown" if !@genre
end
end

p ID3Tag.new(ARGV[0])
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

[QUIZ] ID3 Tags (#136) 33
[SUMMARY] ID3 Tags (#136) 0
How does this work? 2

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top