class M3u8::PlaylistItem

PlaylistItem represents a set of EXT-X-STREAM-INF or EXT-X-I-FRAME-STREAM-INF attributes

Attributes

audio[RW]
audio_codec[RW]
average_bandwidth[RW]
bandwidth[RW]
closed_captions[RW]
codecs[RW]
frame_rate[RW]
hdcp_level[RW]
height[RW]
iframe[RW]
level[RW]
name[RW]
profile[RW]
program_id[RW]
subtitles[RW]
uri[RW]
video[RW]
width[RW]

Public Class Methods

new(params = {}) click to toggle source
# File lib/m3u8/playlist_item.rb, line 12
def initialize(params = {})
  self.iframe = false
  params.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end
parse(text) click to toggle source
# File lib/m3u8/playlist_item.rb, line 19
def self.parse(text)
  item = PlaylistItem.new
  item.parse(text)
  item
end

Public Instance Methods

parse(text) click to toggle source
# File lib/m3u8/playlist_item.rb, line 25
def parse(text)
  attributes = parse_attributes(text)
  options = options_from_attributes(attributes)
  initialize(options)
end
resolution() click to toggle source
# File lib/m3u8/playlist_item.rb, line 31
def resolution
  return if width.nil?
  "#{width}x#{height}"
end
to_s() click to toggle source
# File lib/m3u8/playlist_item.rb, line 55
def to_s
  m3u8_format
end

Private Instance Methods

attributes() click to toggle source
# File lib/m3u8/playlist_item.rb, line 103
def attributes
  [program_id_format,
   resolution_format,
   codecs_format,
   bandwidth_format,
   average_bandwidth_format,
   frame_rate_format,
   hdcp_level_format,
   audio_format,
   video_format,
   subtitles_format,
   closed_captions_format,
   name_format].compact.join(',')
end
audio_codec_code() click to toggle source
# File lib/m3u8/playlist_item.rb, line 182
def audio_codec_code
  return if @audio_codec.nil?
  return 'mp4a.40.2' if @audio_codec.casecmp('aac-lc').zero?
  return 'mp4a.40.5' if @audio_codec.casecmp('he-aac').zero?
  return 'mp4a.40.34' if @audio_codec.casecmp('mp3').zero?
end
audio_format() click to toggle source
# File lib/m3u8/playlist_item.rb, line 152
def audio_format
  return if audio.nil?
  %(AUDIO="#{audio}")
end
average_bandwidth_format() click to toggle source
# File lib/m3u8/playlist_item.rb, line 147
def average_bandwidth_format
  return if average_bandwidth.nil?
  "AVERAGE-BANDWIDTH=#{average_bandwidth}"
end
bandwidth_format() click to toggle source
# File lib/m3u8/playlist_item.rb, line 143
def bandwidth_format
  "BANDWIDTH=#{bandwidth}"
end
baseline_codec_string(level) click to toggle source
# File lib/m3u8/playlist_item.rb, line 197
def baseline_codec_string(level)
  return 'avc1.66.30' if level == 3.0
  return 'avc1.42001f' if level == 3.1
end
closed_captions_format() click to toggle source
# File lib/m3u8/playlist_item.rb, line 167
def closed_captions_format
  return if closed_captions.nil?

  if closed_captions == 'NONE'
    %(CLOSED-CAPTIONS=NONE)
  else
    %(CLOSED-CAPTIONS="#{closed_captions}")
  end
end
codecs_format() click to toggle source
# File lib/m3u8/playlist_item.rb, line 138
def codecs_format
  return if codecs.nil?
  %(CODECS="#{codecs}")
end
frame_rate_format() click to toggle source
# File lib/m3u8/playlist_item.rb, line 128
def frame_rate_format
  return if frame_rate.nil?
  "FRAME-RATE=#{format('%.3f', frame_rate)}"
end
hdcp_level_format() click to toggle source
# File lib/m3u8/playlist_item.rb, line 133
def hdcp_level_format
  return if hdcp_level.nil?
  "HDCP-LEVEL=#{hdcp_level}"
end
high_codec_string(level) click to toggle source
# File lib/m3u8/playlist_item.rb, line 209
def high_codec_string(level)
  return nil unless [3.0, 3.1, 3.2, 4.0, 4.1, 4.2, 5.0, 5.1, 5.2].include?(level)

  level_hex_string = level.to_s.sub('.', '').to_i.to_s(16)
  return "avc1.6400#{level_hex_string}"
end
m3u8_format() click to toggle source
# File lib/m3u8/playlist_item.rb, line 97
def m3u8_format
  return %(#EXT-X-I-FRAME-STREAM-INF:#{attributes},URI="#{uri}") if iframe

  "#EXT-X-STREAM-INF:#{attributes}\n#{uri}"
end
main_codec_string(level) click to toggle source
# File lib/m3u8/playlist_item.rb, line 202
def main_codec_string(level)
  return 'avc1.77.30' if level == 3.0
  return 'avc1.4d001f' if level == 3.1
  return 'avc1.4d0028' if level == 4.0
  return 'avc1.4d0029' if level == 4.1
end
name_format() click to toggle source
# File lib/m3u8/playlist_item.rb, line 177
def name_format
  return if name.nil?
  %(NAME="#{name}")
end
options_from_attributes(attributes) click to toggle source
# File lib/m3u8/playlist_item.rb, line 61
def options_from_attributes(attributes)
  resolution = parse_resolution(attributes['RESOLUTION'])
  { program_id: attributes['PROGRAM-ID'],
    codecs: attributes['CODECS'],
    width: resolution[:width],
    height: resolution[:height],
    bandwidth: attributes['BANDWIDTH'].to_i,
    average_bandwidth:
      parse_average_bandwidth(attributes['AVERAGE-BANDWIDTH']),
    frame_rate: parse_frame_rate(attributes['FRAME-RATE']),
    video: attributes['VIDEO'], audio: attributes['AUDIO'],
    uri: attributes['URI'], subtitles: attributes['SUBTITLES'],
    closed_captions: attributes['CLOSED-CAPTIONS'],
    name: attributes['NAME'], hdcp_level: attributes['HDCP-LEVEL'] }
end
parse_average_bandwidth(value) click to toggle source
# File lib/m3u8/playlist_item.rb, line 77
def parse_average_bandwidth(value)
  value.to_i unless value.nil?
end
parse_frame_rate(frame_rate) click to toggle source
# File lib/m3u8/playlist_item.rb, line 90
def parse_frame_rate(frame_rate)
  return if frame_rate.nil?

  value = BigDecimal(frame_rate)
  value if value > 0
end
parse_resolution(resolution) click to toggle source
# File lib/m3u8/playlist_item.rb, line 81
def parse_resolution(resolution)
  return { width: nil, height: nil } if resolution.nil?

  values = resolution.split('x')
  width = values[0].to_i
  height = values[1].to_i
  { width: width, height: height }
end
program_id_format() click to toggle source
# File lib/m3u8/playlist_item.rb, line 118
def program_id_format
  return if program_id.nil?
  "PROGRAM-ID=#{program_id}"
end
resolution_format() click to toggle source
# File lib/m3u8/playlist_item.rb, line 123
def resolution_format
  return if resolution.nil?
  "RESOLUTION=#{resolution}"
end
subtitles_format() click to toggle source
# File lib/m3u8/playlist_item.rb, line 162
def subtitles_format
  return if subtitles.nil?
  %(SUBTITLES="#{subtitles}")
end
video_codec(profile, level) click to toggle source
# File lib/m3u8/playlist_item.rb, line 189
def video_codec(profile, level)
  return if profile.nil? || level.nil?

  return baseline_codec_string(level) if profile.casecmp('baseline').zero?
  return main_codec_string(level) if profile.casecmp('main').zero?
  return high_codec_string(level) if profile.casecmp('high').zero?
end
video_format() click to toggle source
# File lib/m3u8/playlist_item.rb, line 157
def video_format
  return if video.nil?
  %(VIDEO="#{video}")
end