Class: OggInfo
- Inherits:
-
Object
- Object
- OggInfo
- Extended by:
- Forwardable
- Includes:
- Ogg
- Defined in:
- lib/ogginfo.rb
Constant Summary collapse
- VERSION =
"0.7.2"
Constants included from Ogg
Instance Attribute Summary collapse
-
#channels ⇒ Object
readonly
Returns the value of attribute channels.
-
#nominal_bitrate ⇒ Object
readonly
Returns the value of attribute nominal_bitrate.
-
#samplerate ⇒ Object
readonly
Returns the value of attribute samplerate.
-
#tag ⇒ Object
readonly
tagis a hash containing the vorbis tag like "Artist", "Title", and the like.
Class Method Summary collapse
-
.open(*args) ⇒ Object
"block version" of ::new().
Instance Method Summary collapse
-
#bitrate ⇒ Object
Calculated bit rate, also lazily loaded since we depend on the length.
-
#close ⇒ Object
commits any tags to file.
-
#hastag? ⇒ Boolean
check the presence of a tag.
-
#initialize(filename, charset = nil) ⇒ OggInfo
constructor
create new instance of OggInfo use of charset is deprecated! please use utf-8 encoded strings and leave
charsetto nil"). -
#length ⇒ Object
The length in seconds of the track since this requires reading the whole file we only get it if called.
-
#picture ⇒ Object
get the picture as an array of [extension, file_content] or nil if not existent.
-
#picture=(filepath) ⇒ Object
set a picture (.jpg or .png) on the ogg file.
- #to_s ⇒ Object
Methods included from Ogg
compute_checksum, detect_codec
Constructor Details
#initialize(filename, charset = nil) ⇒ OggInfo
create new instance of OggInfo
use of charset is deprecated! please use utf-8 encoded strings and leave charset to nil")
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# 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., se.backtrace) end end @original_tag = @tag.dup end |
Instance Attribute Details
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
32 33 34 |
# File 'lib/ogginfo.rb', line 32 def channels @channels end |
#nominal_bitrate ⇒ Object (readonly)
Returns the value of attribute nominal_bitrate.
32 33 34 |
# File 'lib/ogginfo.rb', line 32 def nominal_bitrate @nominal_bitrate end |
#samplerate ⇒ Object (readonly)
Returns the value of attribute samplerate.
32 33 34 |
# File 'lib/ogginfo.rb', line 32 def samplerate @samplerate end |
#tag ⇒ Object (readonly)
tag is a hash containing the vorbis tag like "Artist", "Title", and the like
35 36 37 |
# File 'lib/ogginfo.rb', line 35 def tag @tag end |
Class Method Details
.open(*args) ⇒ Object
"block version" of ::new()
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# 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 |
Instance Method Details
#bitrate ⇒ Object
Calculated bit rate, also lazily loaded since we depend on the length
78 79 80 |
# File 'lib/ogginfo.rb', line 78 def bitrate @bitrate ||= (@filesize * 8).to_f / length() end |
#close ⇒ Object
commits any tags to file
149 150 151 152 153 154 155 156 157 158 159 160 161 |
# 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 | (input, tempfile, tag) end tempfile.close FileUtils.cp(tempfile.path, @filename) end end end |
#hastag? ⇒ Boolean
check the presence of a tag
164 165 166 |
# File 'lib/ogginfo.rb', line 164 def hastag? !tag.empty? end |
#length ⇒ Object
The length in seconds of the track since this requires reading the whole file we only get it if called
67 68 69 70 71 72 73 74 |
# File 'lib/ogginfo.rb', line 67 def length unless @length File.open(@filename) do |file| @length = compute_length(file) end end return @length end |
#picture ⇒ Object
get the picture as an array of [extension, file_content] or nil if not existent
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# 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 |
#picture=(filepath) ⇒ Object
set a picture (.jpg or .png) on the ogg file
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# 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 |
#to_s ⇒ Object
168 169 170 |
# File 'lib/ogginfo.rb', line 168 def to_s "channels #{channels} samplerate #{samplerate} bitrate #{nominal_bitrate} #{tag.inspect}" end |