Skip to content

Instantly share code, notes, and snippets.

@alebaffa
Created March 24, 2014 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alebaffa/9741732 to your computer and use it in GitHub Desktop.
Save alebaffa/9741732 to your computer and use it in GitHub Desktop.
Write a Ruby program that analyzes an MP3 file. Many MP3 files have a 128-byte data structure at the end called an ID3 tag. These 128 bytes are literally packed with information about the song: its name, the artist, which album it's from, and so on. You can parse this data structure by opening an MP3 file and doing a series of reads from a posit…
class TextAnalyzer
def initialize
@file = File.open("song.mp3")
end
def has_tag?
@file.seek(-128, IO::SEEK_END)
@file.read(3) == 'TAG'
end
def read_info
if has_tag?
@info = @file.read
@file.close
@info = {
title: @info.byteslice(0, 30),
author: @info.byteslice(30, 30),
album: @info.byteslice(60, 30),
year: @info.byteslice(90, 4),
comment: @info.byteslice(94, 30)
}
else
'File does not contain information'
end
end
private :has_tag?
end
ciao = TextAnalyzer.new
info = ciao.read_info
info.each do|key, value|
puts "#{key} ==> #{value}"
end
@nbnp11
Copy link

nbnp11 commented Jun 24, 2021

on line 23 must be

{ error: 'File does not contain information' }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment