Class: SVT::Recorder::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/svt/recorder/base.rb

Direct Known Subclasses

Play, Rapport

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



44
45
46
47
48
49
50
51
52
# File 'lib/svt/recorder/base.rb', line 44

def initialize(opts={})
  @title      = opts[:title] or ''
  @stream     = opts[:url]
  @base_url   = File.dirname(opts[:url])

  @bitrates   = {} # bitrate => stream
  @part_base  = ''
  @parts      = []
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



55
56
57
# File 'lib/svt/recorder/base.rb', line 55

def base_url
  @base_url
end

#bitrateObject (readonly)

Returns the value of attribute bitrate.



56
57
58
# File 'lib/svt/recorder/base.rb', line 56

def bitrate
  @bitrate
end

#titleObject (readonly)

Returns the value of attribute title.



54
55
56
# File 'lib/svt/recorder/base.rb', line 54

def title
  @title
end

Instance Method Details

#all_partsObject

Yields all parts concatenated with base_url



135
136
137
138
139
# File 'lib/svt/recorder/base.rb', line 135

def all_parts
  parts do |part|
    yield "#{base_url}/#{part}"
  end
end

#bitratesObject

All available bitrates for this video/playlist. Returns:

An array of bitrates, orderered highest->lowest


95
96
97
98
# File 'lib/svt/recorder/base.rb', line 95

def bitrates
  get_streams() if @bitrates.empty?
  @bitrates.keys.sort.reverse
end

#get_streamsObject

– A naïve parser, but until it turns out to be a problem it’ll do. 2012=09-09: If a FQDN address is given only return the basename

The format is:

EXT-X-.... BANDWIDTH=<bitrate>
playlist-filename


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/svt/recorder/base.rb', line 148

def get_streams
  bitrate = nil
  open(@stream).each do |row|
    row.strip!

    if bitrate
      @bitrates[bitrate] = CGI.unescape(row)
      bitrate = nil
    end

    if match = row.match(/#EXT-X-STREAM-INF:.+BANDWIDTH=(.+) ?$/)
      bitrate = match[1].to_i
    end
  end
end

#last_partObject



58
# File 'lib/svt/recorder/base.rb', line 58

def last_part ; @parts.size ; end

#part_urls(bitrate = nil) ⇒ Object

All part URL:s for a specific bitrate

Args:

bitrate :: The bitrate for which to fetch the parts,
             defaults to the highest bitrate available.

Returns:

self


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/svt/recorder/base.rb', line 68

def part_urls(bitrate = nil)
  @bitrate = if bitrate.nil?
               get_streams() if @bitrates.empty?
               @bitrates.keys.max
             else
               bitrate
             end

  url = @bitrates[@bitrate]
  unless url.match(/^http/)
    url = File.join(@base_url, @bitrates[@bitrate])
  end

  open(url).each do |row|
    next if row[0..0] == '#'
    row.strip!

    @part_base = File.dirname(row) if @part_base.empty?
    @parts << File.basename(row)
  end

  self
end

#partsObject

Returns or yields all parts, in order, for this video. If all parts then are downloaded in sequence and concatenated there will be a playable movie.

Yield:

A complete part download URL

Returns:

All parts in an ordered array, first -> last, full URL


109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/svt/recorder/base.rb', line 109

def parts
  part_urls() if last_part == 0

  if block_given?
    @parts.each do |i|
      yield "#{@part_base}/#{i}"
    end
  else
    # I want you Object#tap
    tmp = []
    parts {|part| tmp << part }
    tmp
  end
end

#parts?Boolean

Returns the number of parts this recording got

Returns:

int the numbers of parts, 0 index

Returns:

  • (Boolean)


128
129
130
131
132
# File 'lib/svt/recorder/base.rb', line 128

def parts?
  part_urls() if last_part == 0

  return last_part
end