Class: WebVideo::Transcoder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath, options = {}, adapter = :ffmpeg) ⇒ Transcoder

Video Convertation

transcoder = WebVideo::Transcoder.new(“demo.avi”) transcoder.source # WebVideo::Adapters::AbstractAdapter instance

Or

video = WebVideo::Adapters::FfmpegAdapter.new(‘demo.avi’) transcoder = WebVideo::Transcoder.new(video) transcoder.source # WebVideo::Adapters::FfmpegAdapter instance (video)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/web_video/transcoder.rb', line 17

def initialize(filepath, options = {}, adapter = :ffmpeg)
  @adapter = adapter
  
  if filepath.is_a?(WebVideo::Adapters::AbstractAdapter)
    @source = filepath
  else
    args = [filepath, options]
    
    @source = case @adapter
      when String, Symbol then
        load_adapter(@adapter.to_s).new(*args)
      when Class then
        @adapter.new(*args)
      else
        @adapter
      end
    end
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

#sourceObject

Returns the value of attribute source.



3
4
5
# File 'lib/web_video/transcoder.rb', line 3

def source
  @source
end

Instance Method Details

#convert(destination, options = {}, &block) ⇒ Object

Generate new video file

transcoder = WebVideo::Transcoder.new(“demo.avi”)

begin

 transcoder.convert("demo.flv", :resolution => "480x360") do |command|
   command << "-ar 22050"
   command << "-ab 128k"
   command << "-acodec libmp3lame"
   command << "-vcodec flv"
   command << "-r 25"
   command << "-y"
 end
rescue WebVideo::CommandLineError => e
  WebVideo.logger.error("Unable to transcode video: #{e.class} - #{e.message}")

end

options:

:resolution - video resolution


81
82
83
84
85
# File 'lib/web_video/transcoder.rb', line 81

def convert(destination, options = {}, &block)
  options.symbolize_keys!
 
  process(destination, @source.convert_command, options, &block)
end

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

Execute command



89
90
91
# File 'lib/web_video/transcoder.rb', line 89

def execute(command, options = {})
  @source.run(command, options)
end

#screenshot(destination, options = {}, &block) ⇒ Object

Create screenshots

transcoder = WebVideo::Transcoder.new(“demo.avi”) transcoder.screenshot(“demo.jpg”, :resolution => “480x360”)

options:

:count  - count images to generate
:format - image decoder
:at     - sets that image should be taken from the point x seconds from the beginning
:resolution - image resolution


46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/web_video/transcoder.rb', line 46

def screenshot(destination, options = {}, &block)
  options = { 
    :count => 1, 
    :format => "image2", 
    :at => "00:00:01" 
  }.merge(options.symbolize_keys)
  
  # Calculate middle video position
  if options[:at].is_a?(Symbol) && [:center, :middle].include?(options[:at])
    options[:at] = @source.duration_in_seconds / 2
  end
  
  process(destination, @source.screenshot_command, options, &block)
end