Object
create new instance of OggInfo use of charset is deprecated! please use utf-8 encoded strings and leave charset to nil“)
# File lib/ogginfo.rb, line 39 def initialize(filename, charset = nil) if charset warn("use of charset is deprecated! please use utf-8 encoded tags") end @filename = filename @length = nil @bitrate = nil File.open(@filename, 'rb') do |file| begin info = read_headers(file) @samplerate = info[:samplerate] @nominal_bitrate = info[:nominal_bitrate] @channels = info[:channels] @tag = info[:tag] # filesize is used to calculate bitrate # but we don't want to include the headers @filesize = file.stat.size - file.pos rescue Ogg::StreamError => se raise(OggInfoError, se.message, se.backtrace) end end @original_tag = @tag.dup end
“block version” of ::new()
# File lib/ogginfo.rb, line 133 def self.open(*args) m = self.new(*args) ret = nil if block_given? begin ret = yield(m) ensure m.close end else ret = m end ret end
Calculated bit rate, also lazily loaded since we depend on the length
# File lib/ogginfo.rb, line 78 def bitrate @bitrate ||= (@filesize * 8).to_f / length() end
commits any tags to file
# File lib/ogginfo.rb, line 149 def close if tag != @original_tag Tempfile.open(["ruby-ogginfo", ".ogg"]) do |tempfile| tempfile.close tempfile = File.new(tempfile.path, "wb") File.open(@filename, "rb") do | input | replace_tags(input, tempfile, tag) end tempfile.close FileUtils.cp(tempfile.path, @filename) end end end
check the presence of a tag
# File lib/ogginfo.rb, line 164 def hastag? !tag.empty? end
The length in seconds of the track since this requires reading the whole file we only get it if called
# File lib/ogginfo.rb, line 67 def length unless @length File.open(@filename) do |file| @length = compute_length(file) end end return @length end
get the picture as an array of [extension, file_content] or nil if not existent
# File lib/ogginfo.rb, line 109 def picture extensions = { "image/jpeg" => ".jpg", "image/png" => ".png" } if content = tag["metadata_block_picture"] _, #type, # picture type _, # mime_type size mime_type, _, # description size _, # description, _, # width _, # height _, # color depth _, # number of color used _, #file_content_size, file_content = content.unpack("m*").first.unpack("NNa10Na10NNNNNa*") return [extensions[mime_type], file_content] end nil end
set a picture (.jpg or .png) on the ogg file
# File lib/ogginfo.rb, line 83 def picture=(filepath) ext = File.extname(filepath) mime_type = { ".jpg" => "image/jpeg", ".png" => "image/png" }[ext] description = "folder#{ext}" raw_string = [3, # picture type mime_type.size, mime_type, description.size, description, 0, # width 0, # height 0, # color depth 0, # number of colors used File.size(filepath), File.binread(filepath) ].pack("NNa*Na*NNNNNa*") @tag["METADATA_BLOCK_PICTURE"] = [raw_string].pack("m*").strip end
Generated with the Darkfish Rdoc Generator 2.