Parse a mp3 file : mp3 file « File Directory « Ruby






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')

 








Related examples in the same category

1.Parse a mp3 file and returns [track, artist, album, year, comment, genre]