Sha256: 020ffe27304886885fe3a68f52527a73de18a697221713cc636d799c83e379ba

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

# typed: true
# frozen_string_literal: true

module Ffprober
  class Wrapper
    extend T::Sig

    attr_reader :json

    sig { returns(Ffprober::Format) }
    attr_reader :format

    # rubocop:disable Metrics/AbcSize
    sig { params(json: T::Hash[T.untyped, T.untyped]).void }
    def initialize(json)
      raise FfprobeError, json[:error] if json[:error]

      @json = json
      @format = T.let(Format.new(json[:format]), Ffprober::Format)
      @video_streams = T.let(nil, T.nilable(T::Array[Ffprober::VideoStream]))
      @audio_streams = T.let(nil, T.nilable(T::Array[Ffprober::AudioStream]))
      @data_streams = T.let(nil, T.nilable(T::Array[Ffprober::DataStream]))
      @subtitle_streams = T.let(nil, T.nilable(T::Array[Ffprober::SubtitleStream]))
      @chapters = T.let(nil, T.nilable(T::Array[Ffprober::Chapter]))
    end
    # rubocop:enable Metrics/AbcSize

    sig { returns(T::Array[Ffprober::VideoStream]) }
    def video_streams
      @video_streams ||= stream_by_codec('video').map do |data|
        VideoStream.new(data)
      end
    end

    sig { returns(T::Array[Ffprober::AudioStream]) }
    def audio_streams
      @audio_streams ||= stream_by_codec('audio').map do |data|
        AudioStream.new(data)
      end
    end

    sig { returns(T::Array[Ffprober::DataStream]) }
    def data_streams
      @data_streams ||= stream_by_codec('data').map do |data|
        DataStream.new(data)
      end
    end

    sig { returns(T::Array[Ffprober::Chapter]) }
    def chapters
      @chapters ||= json[:chapters].map { |chapter| Chapter.new(chapter) }
    end

    sig { returns(T::Array[Ffprober::SubtitleStream]) }
    def subtitle_streams
      @subtitle_streams ||= stream_by_codec('subtitle').map do |stream|
        SubtitleStream.new(stream)
      end
    end

    private

    def stream_by_codec(codec_type)
      streams.select { |stream| stream[:codec_type] == codec_type }
    end

    def streams
      json[:streams]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ffprober-1.0 lib/ffprober/wrapper.rb