Class: FfmpegWrapper::FFprobe

Inherits:
Object
  • Object
show all
Defined in:
lib/ffmpeg_wrapper/ffprobe.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ FFprobe

Returns a new instance of FFprobe.

Parameters:

  • filename (String)

42
43
44
45
46
47
# File 'lib/ffmpeg_wrapper/ffprobe.rb', line 42

def initialize(filename)
  @input = filename || fail(ArgumentError 'No input specified')
  @command = 'ffprobe '
  @show_specifiers = []
  @options = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object (private)


58
59
60
61
62
63
64
65
# File 'lib/ffmpeg_wrapper/ffprobe.rb', line 58

def method_missing(meth, *args, &blk)
  case meth
  when /show_(\w+)/ then
    @show_specifiers << meth
  else
    super
  end
end

Class Method Details

.run(filename, &block) ⇒ Hash

Execute ffprobe command.

Examples:

info = FFprobe.run('video.mp4') do
  show_streams
  show_format
end
info                         #=> { "format" => ..., "streams"=>... }
info['format']['codec_type'] #=> 'video'

Parameters:

  • filename (String)

Returns:


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ffmpeg_wrapper/ffprobe.rb', line 16

def run(filename, &block)
  ff = FFprobe.new filename
  ff.instance_eval do
    @command << ' -v quiet -of json'
    instance_eval &block if block
    @command << @show_specifiers.reduce(' ') do |acc, v|
      acc << " -#{v}"
      acc
    end
    @command << ' ' << @options.to_shellflags
    @command << ' ' << @input
    out = `#{@command} 2>/dev/null`
    begin
      @result = JSON.parse out
      fail if @result.keys.empty?
    rescue
      @result = { 'errors' => error }
    # FIXME: Do not return from ensure
    ensure
      return @result
    end
  end
end

Instance Method Details

#options(hash) ⇒ Object Also known as: option

Specify input file options

Parameters:


51
52
53
# File 'lib/ffmpeg_wrapper/ffprobe.rb', line 51

def options(hash)
  @options.merge! hash
end