Sha256: f5790009d404f854f883fee9a4a586b858620f75ac188fdec5a59336e3211f29

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

module Ffprober
  class Parser
    @@options = '-v quiet -print_format json -show_format -show_streams'

    class << self
      def from_file(file_to_parse)
        unless FfprobeVersion.valid?
          raise ArgumentError.new("no or unsupported ffprobe version found.\
                                  (version: #{FfprobeVersion.parsed_version.to_s})")
        end

        json_output = `#{Ffprober.path} #{@@options} #{file_to_parse}`
        from_json(json_output)
      end

      def from_json(json_to_parse)
        parser = self.new
        parser.parse(json_to_parse)
        parser
      end
    end

    def parse(json_to_parse)
      raise ArgumentError.new("No JSON found") if json_to_parse.nil?
      @json_to_parse = json_to_parse
    end

    def parsed_json
      @parsed_json ||= JSON.parse(@json_to_parse, symbolize_names: true)
    end

    def format
      @format ||= Ffprober::Format.new(parsed_json[:format])
    end

    def video_streams
      @video_streams ||= stream_by_codec('video').map { |s| Ffprober::VideoStream.new(s) }
    end

    def audio_streams
      @audio_streams ||= stream_by_codec('audio').map { |s| Ffprober::AudioStream.new(s) }
    end

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

    def streams
      parsed_json[:streams]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ffprober-0.2.2 lib/ffprober/parser.rb