Class: RVideo::Transcoder

Inherits:
Object
  • Object
show all
Defined in:
lib/rvideo/transcoder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTranscoder

To transcode a video, initialize a Transcoder object:

transcoder = RVideo::Transcoder.new

Then pass a recipe and valid options to the execute method

recipe = "ffmpeg -i $input_file$ -ar 22050 -ab 64 -f flv -r 29.97 -s"
recipe += " $resolution$ -y $output_file$"
recipe += "\nflvtool2 -U $output_file$"
begin
  transcoder.execute(recipe, {:input_file => "/path/to/input.mp4",
    :output_file => "/path/to/output.flv", :resolution => "640x360"})
rescue TranscoderError => e
  puts "Unable to transcode file: #{e.class} - #{e.message}"
end

If the job succeeds, you can access the metadata of the input and output files with:

transcoder.original     # RVideo::Inspector object
transcoder.processed    # RVideo::Inspector object

If the transcoding succeeds, the file may still have problems. RVideo will populate an errors array if the duration of the processed video differs from the duration of the original video, or if the processed file is unreadable.



35
36
37
38
39
# File 'lib/rvideo/transcoder.rb', line 35

def initialize
  @executed_commands = []
  @errors = []
  @warnings = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/rvideo/transcoder.rb', line 4

def errors
  @errors
end

#executed_commandsObject (readonly)

Returns the value of attribute executed_commands.



4
5
6
# File 'lib/rvideo/transcoder.rb', line 4

def executed_commands
  @executed_commands
end

#metadataObject (readonly)

Returns the value of attribute metadata.



4
5
6
# File 'lib/rvideo/transcoder.rb', line 4

def 
  @metadata
end

#originalObject (readonly)

Returns the value of attribute original.



4
5
6
# File 'lib/rvideo/transcoder.rb', line 4

def original
  @original
end

#processedObject (readonly)

Returns the value of attribute processed.



4
5
6
# File 'lib/rvideo/transcoder.rb', line 4

def processed
  @processed
end

#total_timeObject (readonly)

Returns the value of attribute total_time.



4
5
6
# File 'lib/rvideo/transcoder.rb', line 4

def total_time
  @total_time
end

#warningsObject (readonly)

Returns the value of attribute warnings.



4
5
6
# File 'lib/rvideo/transcoder.rb', line 4

def warnings
  @warnings
end

Class Method Details

.loggerObject



52
53
54
55
56
57
58
# File 'lib/rvideo/transcoder.rb', line 52

def self.logger
  if @logger.nil?
    @logger = Logger.new('/dev/null')
  end
  
  @logger
end

.logger=(l) ⇒ Object

Configure logging. Pass a valid Ruby logger object.

logger = Logger.new(STDOUT)
RVideo::Transcoder.logger = logger


48
49
50
# File 'lib/rvideo/transcoder.rb', line 48

def self.logger=(l)
  @logger = l
end

Instance Method Details

#execute(task, options = {}) ⇒ Object

Requires a command and a hash of various interpolated options. The command should be one or more lines of transcoder tool commands (e.g. ffmpeg, flvtool2). Interpolate options by adding $option_key$ to the recipe, and passing :option_key => “value” in the options hash.

recipe = "ffmpeg -i $input_file$ -ar 22050 -ab 64 -f flv -r 29.97 
recipe += "-s $resolution$ -y $output_file$"
recipe += "\nflvtool2 -U $output_file$"

begin
  transcoder.execute(recipe, {:input_file => "/path/to/input.mp4",
    :output_file => "/path/to/output.flv", :resolution => "320x240"})
rescue TranscoderError => e
  puts "Unable to transcode file: #{e.class} - #{e.message}"
end


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rvideo/transcoder.rb', line 78

def execute(task, options = {})
  t1 = Time.now
  Transcoder.logger.info("\nNew transcoder job\n================\nTask: #{task}\nOptions: #{options.inspect}")
  @original = Inspector.new(:file => options[:input_file])
  @metadata = @original.raw_response
  
  if task.class == String
    parse_and_execute(task, options)
  else
    raise ArgumentError, "first argument must be a recipe string, but got a #{task.class.to_s} (#{task})"
  end
  @processed = Inspector.new(:file => options[:output_file])
  result = check_integrity
  Transcoder.logger.info("\nFinished task. Total errors: #{@errors.size}\n")
  @total_time = Time.now - t1
  result
rescue TranscoderError => e
  raise e
rescue Exception => e
  Transcoder.logger.error("[ERROR] Unhandled RVideo exception: #{e.class} - #{e.message}\n#{e.backtrace}")
  raise TranscoderError::UnknownError, "#{e.class} - #{e.message}"
end