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
53
# 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  = ''
  @first_part = 0
  @last_part  = 0
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#bitrateObject (readonly)

Returns the value of attribute bitrate.



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

def bitrate
  @bitrate
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#all_partsObject

Yields all parts concatenated with base_url



131
132
133
134
135
# File 'lib/svt/recorder/base.rb', line 131

def all_parts
  parts do |part|
    yield File.join(base_url, part)
  end
end

#bitratesObject

All available bitrates for this video/playlist. Returns:

An array of bitrates, orderered highest->lowest


91
92
93
94
# File 'lib/svt/recorder/base.rb', line 91

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


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/svt/recorder/base.rb', line 144

def get_streams
  bitrate = nil
  open(@stream).each do |row|
    row.strip!
    row = File.basename(row) if row.match(/^http/)

    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

#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


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

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

  url = File.join(@base_url, @bitrates[@bitrate])

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

    @part_base = File.dirname(row) if @part_base.empty?
    @last_part += 1
  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


105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/svt/recorder/base.rb', line 105

def parts
  part_urls() if @last_part == 0

  if block_given?
    (@first_part...@last_part).each do |i|
      yield "#{@part_base}/segment#{i}.ts"
    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)


124
125
126
127
128
# File 'lib/svt/recorder/base.rb', line 124

def parts?
  part_urls() if @last_part == 0

  return @last_part
end