Sha256: b236d478d331f818e9658ee5282bdb7b28ff115152c2cbb45d64adeafc0a6a93

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true
module M3u8
  # Playlist represents an m3u8 playlist, it can be a master playlist or a set
  # of media segments
  class Playlist
    attr_accessor :items, :version, :cache, :target, :sequence, :type,
                  :iframes_only, :independent_segments

    def initialize(options = {})
      assign_options options
      self.items = []
    end

    def self.codecs(options = {})
      item = PlaylistItem.new options
      item.codecs
    end

    def self.read(input)
      reader = Reader.new
      reader.read input
    end

    def write(output)
      writer = Writer.new(output)
      writer.write(self)
    end

    def master?
      return false if playlist_size == 0 && segment_size == 0
      playlist_size > 0
    end

    def to_s
      output = StringIO.open
      write(output)
      output.string
    end

    def valid?
      return false if playlist_size > 0 && segment_size > 0
      true
    end

    def duration
      duration = 0.0
      items.each do |item|
        duration += item.duration if item.is_a?(M3u8::SegmentItem)
      end
      duration
    end

    private

    def assign_options(options)
      options = defaults.merge(options)

      self.version = options[:version]
      self.sequence = options[:sequence]
      self.cache = options[:cache]
      self.target = options[:target]
      self.type = options[:type]
      self.iframes_only = options[:iframes_only]
      self.independent_segments = options[:independent_segments]
    end

    def defaults
      {
        sequence: 0,
        target: 10,
        iframes_only: false,
        independent_segments: false
      }
    end

    def playlist_size
      items.count { |item| item.is_a?(PlaylistItem) }
    end

    def segment_size
      items.count { |item| item.is_a?(SegmentItem) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
m3u8-0.7.1 lib/m3u8/playlist.rb