Class: Ffprober::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ffprober/parser.rb

Constant Summary collapse

@@options =
'-v quiet -print_format json -show_format -show_streams'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_file(file_to_parse) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/ffprober/parser.rb', line 6

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

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

.from_json(json_to_parse) ⇒ Object



16
17
18
19
20
# File 'lib/ffprober/parser.rb', line 16

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

Instance Method Details

#audio_streamsObject



44
45
46
# File 'lib/ffprober/parser.rb', line 44

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

#formatObject



36
37
38
# File 'lib/ffprober/parser.rb', line 36

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

#parse(json_to_parse) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
# File 'lib/ffprober/parser.rb', line 23

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

#parsed_jsonObject



28
29
30
31
32
33
34
# File 'lib/ffprober/parser.rb', line 28

def parsed_json
  @parsed_json ||=  begin
                      json = JSON.parse(@json_to_parse, symbolize_names: true)
                      raise InvalidInputFileError.new("Invalid input file") if json.empty?
                      json
                    end
end

#stream_by_codec(codec_type) ⇒ Object



48
49
50
# File 'lib/ffprober/parser.rb', line 48

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

#streamsObject



52
53
54
# File 'lib/ffprober/parser.rb', line 52

def streams
  parsed_json[:streams]
end

#video_streamsObject



40
41
42
# File 'lib/ffprober/parser.rb', line 40

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