Class: Ogg::Info

Inherits:
Object
  • Object
show all
Defined in:
lib/ogginfo-rb.rb

Overview

See README for usage information.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Info

Returns a new instance of Info.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ogginfo-rb.rb', line 12

def initialize path
  raise ArgumentError.new "Argument is not a string" unless path.respond_to? :to_str
  
  raise Ogg::ReadError.new "File does not exist" unless File.exists? path
  file = File.new(path.to_str, 'rb')
  raise Ogg::ReadError.new "File is empty" if file.stat.size == 0
  
  first_page = Page.find_page(file)
  raise Ogg::ReadError.new "File does not contain valid Ogg data" unless first_page

  # Identification header
  id_packet = Vorbis::IdentificationHeaderPacket.new(first_page)
  @sample_rate = id_packet.audio_sample_rate
  @nominal_bitrate = id_packet.bitrate_nominal
  @channels = id_packet.audio_channels

  # Comment header
  comment_packet = Vorbis::CommentHeaderPacket.new(id_packet.next_page)
  @comments = comment_packet.comments

  pos_after_headers = file.pos

  begin
    @duration = calculate_duration file
  rescue Exception
    @duration = 0
  end

  begin
    @bitrate = (file.stat.size - pos_after_headers).to_f * 8 / @duration
  rescue Exception
    @bitrate = 0
  end

  file.close
end

Instance Attribute Details

#bitrateObject (readonly)

Returns the value of attribute bitrate.



10
11
12
# File 'lib/ogginfo-rb.rb', line 10

def bitrate
  @bitrate
end

#channelsObject (readonly)

Returns the value of attribute channels.



10
11
12
# File 'lib/ogginfo-rb.rb', line 10

def channels
  @channels
end

#commentsObject (readonly)

Returns the value of attribute comments.



9
10
11
# File 'lib/ogginfo-rb.rb', line 9

def comments
  @comments
end

#durationObject (readonly)

Returns the value of attribute duration.



10
11
12
# File 'lib/ogginfo-rb.rb', line 10

def duration
  @duration
end

#nominal_bitrateObject (readonly)

Returns the value of attribute nominal_bitrate.



10
11
12
# File 'lib/ogginfo-rb.rb', line 10

def nominal_bitrate
  @nominal_bitrate
end

#sample_rateObject (readonly)

Returns the value of attribute sample_rate.



10
11
12
# File 'lib/ogginfo-rb.rb', line 10

def sample_rate
  @sample_rate
end

Class Method Details

.open(path) {|info| ... } ⇒ Object

Get information about the Ogg file at the path specified. An Ogg::Info object is returned and also passed to the block if one is provided.

Yields:

  • (info)


51
52
53
54
55
# File 'lib/ogginfo-rb.rb', line 51

def self.open(path)
  info = Ogg::Info.new(path)
  yield info if block_given?
  return info
end