Class: RVideo::Transcoder
- Inherits:
-
Object
- Object
- RVideo::Transcoder
- Defined in:
- lib/rvideo/transcoder.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#executed_commands ⇒ Object
readonly
Returns the value of attribute executed_commands.
-
#processed ⇒ Object
readonly
Returns the value of attribute processed.
-
#total_time ⇒ Object
readonly
Returns the value of attribute total_time.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Instance Method Summary collapse
-
#execute(task, options = {}) ⇒ Object
Requires a command and a hash of various interpolated options.
-
#initialize(input_file = nil) ⇒ Transcoder
constructor
To transcode a video, initialize a Transcoder object:.
- #original ⇒ Object
Constructor Details
#initialize(input_file = nil) ⇒ Transcoder
To transcode a video, initialize a Transcoder object:
transcoder = RVideo::Transcoder.new("/path/to/input.mov")
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, {:output_file => "/path/to/output.flv",
:resolution => "640x360"})
rescue TranscoderError => e
puts "Unable to transcode file: #{e.class} - #{e.}"
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 40 41 42 43 |
# File 'lib/rvideo/transcoder.rb', line 35 def initialize(input_file = nil) # Allow a nil input_file for backwards compatibility. (Change at 1.0?) check_input_file(input_file) @input_file = input_file @executed_commands = [] @errors = [] @warnings = [] end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
4 5 6 |
# File 'lib/rvideo/transcoder.rb', line 4 def errors @errors end |
#executed_commands ⇒ Object (readonly)
Returns the value of attribute executed_commands.
4 5 6 |
# File 'lib/rvideo/transcoder.rb', line 4 def executed_commands @executed_commands end |
#processed ⇒ Object (readonly)
Returns the value of attribute processed.
4 5 6 |
# File 'lib/rvideo/transcoder.rb', line 4 def processed @processed end |
#total_time ⇒ Object (readonly)
Returns the value of attribute total_time.
4 5 6 |
# File 'lib/rvideo/transcoder.rb', line 4 def total_time @total_time end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
4 5 6 |
# File 'lib/rvideo/transcoder.rb', line 4 def warnings @warnings 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$"
transcoder = RVideo::Transcoder.new("/path/to/input.mov")
begin
transcoder.execute(recipe, {:output_file => "/path/to/output.flv", :resolution => "320x240"})
rescue TranscoderError => e
puts "Unable to transcode file: #{e.class} - #{e.message}"
end
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rvideo/transcoder.rb', line 67 def execute(task, = {}) [:progress_sample_rate] ||= 5 [:progress_timeout] ||= false t1 = Time.now if @input_file.nil? @input_file = [:input_file] end RVideo.logger.info("\nNew transcoder job\n================\nTask: #{task}\nOptions: #{.inspect}") if block_given? parse_and_execute(task, ) do |tool, progress| yield(tool, progress) end else parse_and_execute(task, ) end @processed = Inspector.new(:file => [:output_file]) result = check_integrity RVideo.logger.info("\nFinished task. Total errors: #{@errors.size}\n") @total_time = Time.now - t1 result rescue TranscoderError => e raise e rescue Exception => e RVideo.logger.error "[ERROR] Unhandled RVideo exception: #{e.class} - #{e.}" RVideo.logger.error e.backtrace.join("\n\t") raise TranscoderError::UnknownError, "Unexpected RVideo error: #{e.} (#{e.class})" end |