Sha256: b08e86910502ae02d39cebda39feb69fc07d21de7915cfd103a90d6b772019dd

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

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

    def self.from_file(file_to_parse)
      raise ArgumentError.new("no or unsupported ffprobe version found. (version: #{ffprobe_version})") unless ffprobe_version_valid?

      json_output = `#{ffprobe_path} #{@@options} #{file_to_parse}`
      from_json(json_output)
    end

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

    def parse(json_to_parse)
      raise ArgumentError.new("No JSON found") if json_to_parse.nil?
      @video_json = MultiJson.load(json_to_parse, :symbolize_keys => true)
    end

    def format
      Ffprober::Format.new(@video_json[:format])
    end

    def video_streams
      @video_json[:streams].select { |stream| stream[:codec_type] == 'video'}.map do |s|
        Ffprober::VideoStream.new(s)
      end
    end

    def audio_streams
      @video_json[:streams].select { |stream| stream[:codec_type] == 'audio'}.map do |s|
        Ffprober::AudioStream.new(s)
      end
    end

    def self.ffprobe_version_valid?
      ffprobe_version[:minor] > 9
    end

    def self.ffprobe_version
      version = `#{ffprobe_path} -version`.match(/^ffprobe version (?:(\d+)\.)?(?:(\d+)\.)?(\*|\d+)$/)
      raise Errno::ENOENT  if version.nil?
      major, minor, patch = version[1].to_i, version[2].to_i, version[3].to_i
      {major: major, minor: minor, patch: patch}
    rescue Errno::ENOENT => e
      {major: 0, minor: 0, patch: 0}
    end

    def self.ffprobe_path
      name = 'ffprobe'

      if File.executable? name
        cmd
      else
        path = ENV['PATH'].split(File::PATH_SEPARATOR).find { |path|
          File.executable? File.join(path, name)
        }
        path && File.expand_path(name, path)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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