Class: RubberBand::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/rubberband/processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(input_file, output_file, options = Options.new) ⇒ Processor

Returns a new instance of Processor.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rubberband/processor.rb', line 6

def initialize(input_file, output_file, options = Options.new)
  @input_file = input_file
  @output_file = output_file
  @errors = []

  if options.is_a?(String) || options.is_a?(Options)
    @raw_options = options
  elsif options.is_a?(Hash)
    @raw_options = Options.new(options)
  else
    raise ArgumentError, "Unknown options format '#{options.class}', should be either EncodingOptions, Hash or String."
  end
end

Instance Method Details

#binaryObject



20
21
22
23
24
25
26
27
# File 'lib/rubberband/processor.rb', line 20

def binary
  binary_path = `which rubberband`
  if $? == 0
    binary_path
  else
    raise "rubberband binary not found!"
  end
end

#convert_succeeded?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/rubberband/processor.rb', line 54

def convert_succeeded?
  unless File.exists?(@output_file)
    @errors << "no output file created"
    return false
  end
  true
end

#runObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rubberband/processor.rb', line 29

def run
  command = "#{binary} #{@raw_options} '#{Shellwords.escape(@input_file)}' '#{Shellwords.escape(@output_file)}'"
  output  = ""

  Open3.popen3(command) do |stdin, stdout, stderr|
    yield(0.0) if block_given?
    
    stderr.each("r") do |line| 
      output << line
      if line =~ /(\d+)%/ # ffmpeg 0.8 and above style
        progress = $1.to_f / 100.0
        yield(progress)
      end
    end
  end
  
  if convert_succeeded?
    yield(1.0)
    true
  else
    errors = @errors.empty? ? "" : " Errors: #{@errors.join(", ")}. "
    raise "Failed encoding.#{errors}Full output: #{output}"
  end
end