Module: RVideo::Tools::AbstractTool::InstanceMethods

Included in:
Ffmpeg, Flvtool2, Mencoder
Defined in:
lib/rvideo/tools/abstract_tool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



17
18
19
# File 'lib/rvideo/tools/abstract_tool.rb', line 17

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/rvideo/tools/abstract_tool.rb', line 17

def options
  @options
end

#raw_resultObject (readonly)

Returns the value of attribute raw_result.



17
18
19
# File 'lib/rvideo/tools/abstract_tool.rb', line 17

def raw_result
  @raw_result
end

Instance Method Details

#executeObject

Execute the command and parse the result.



62
63
64
65
66
67
68
# File 'lib/rvideo/tools/abstract_tool.rb', line 62

def execute
  final_command = "#{@command} 2>&1"
  Transcoder.logger.info("\nExecuting Command: #{final_command}\n")
  @raw_result = `#{final_command}`
  Transcoder.logger.info("Result: \n#{@raw_result}")
  parse_result(@raw_result)
end

#initialize(raw_command, options = {}) ⇒ Object



19
20
21
22
23
# File 'lib/rvideo/tools/abstract_tool.rb', line 19

def initialize(raw_command, options = {})
  @raw_command = raw_command
  @options = HashWithIndifferentAccess.new(options)
  @command = interpolate_variables(raw_command)
end

#interpolate_variables(raw_command) ⇒ Object

Look for variables surrounded by $, and interpolate with either variables passed in the options hash, or special methods provided by the tool class (e.g. “$original_fps$” with ffmpeg).

$foo$ should match $foo or $foo$ or $foo$ should not



33
34
35
36
37
38
39
# File 'lib/rvideo/tools/abstract_tool.rb', line 33

def interpolate_variables(raw_command)
  raw_command.scan(/[^\\]\$[-_a-zA-Z]+\$/).each do |match|
    match.strip!
    raw_command.gsub!(match, matched_variable(match))
  end
  raw_command.gsub("\\$", "$")
end

#matched_variable(match) ⇒ Object

Strip the $s. First, look for a supplied option that matches the variable name. If one is not found, look for a method that matches. If not found, raise ParameterError exception.



47
48
49
50
51
52
53
54
55
56
# File 'lib/rvideo/tools/abstract_tool.rb', line 47

def matched_variable(match)
  variable_name = match.gsub("$","")
  if @options.key?(variable_name) 
    @options[variable_name] || ""
  elsif self.respond_to? variable_name
    self.send(variable_name)
  else
    raise TranscoderError::ParameterError, "command is looking for the #{variable_name} parameter, but it was not provided. (Command: #{@raw_command})"
  end
end