Created
March 24, 2014 14:56
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
on line 23 must be
{ error: 'File does not contain information' }