Ruby/File Directory/mp3 file
Материал из Wiki.crossplatform.ru
Parse a mp3 file
def parse_id3(mp3_file) fields_and_sizes = [[:track_name, 30], [:artist_name, 30], [:album_name, 30], [:year, 4], [:comment, 30], [:genre, 1]] tag = {} open(mp3_file) do |f| f.seek(-128, File::SEEK_END) if f.read(3) == "TAG" # An ID3 tag is present fields_and_sizes.each do |field, size| data = f.read(size).gsub(/\000.*/, "") data = data[0] if field == :genre tag[field] = data end end end return tag end parse_id3("ID3.mp3")
Parse a mp3 file and returns [track, artist, album, year, comment, genre]
def parse_id3(mp3_file) format = "Z30Z30Z30Z4Z30C" open(mp3_file) do |f| f.seek(-128, File::SEEK_END) if f.read(3) == "TAG" # An ID3 tag is present return f.read(125).unpack(format) end end return nil end parse_id3("ID3.mp3")