#!/usr/bin/ruby -w #
# Ruby ID3 Tag v1.1 Library #
# Version: 0.90 #
# (c)2006 Suninny's Rails Theme #
# http://blog.youkuaiyun.com/rails #
#####################################

class ID3
def initialize(filename)
@filename = filename
begin
File.open(filename) do |file|
file.seek(-128, IO::SEEK_END)
data = file.read
tag, title, artist, album, year, comment, genre =
data.unpack("A3A30A30A30A4A30C1")
if tag != 'TAG'
clear
else
@title, @artist, @album, @year, @comment, @genre =
title, artist, album, year, comment, genre
end
end
rescue
clear
end
end

attr_accessor :title, :artist, :album, :comment, :year, :genre

def write
File.open(@filename, "r+") do |file|
file.seek(-128, IO::SEEK_END)
data = file.read
tag, title, artist, album, year, comment, genre =
data.unpack("A3A30A30A30A4A30C1")
if tag == 'TAG'
file.seek(-128, IO::SEEK_END)
else
file.seek(0, IO::SEEK_END)
end
file.write bin
end
end

def bin
title = @title + (" " * 30)
artist = @artist + (" " * 30)
album = @album + (" " * 30)
comment = @comment + (" " * 30)
['TAG', title, artist, album, @year, comment, @genre].
pack("a3a30a30a30a4a30C1")
end

def clear
@title=@artist=@album=@year=@comment = ''
@genre = 0
end
end

用wxRuby作了个简易界面: