class ActiveJob::Ffmpeg::Encoder::Base

Attributes

audio_bitrate[R]
audio_sample_rate[R]
input_filename[R]
on_complete[R]
on_progress[R]
other_options[R]
output_filename[R]
size[R]
video_bitrate[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/active_job/ffmpeg/encoder/base.rb, line 12
def initialize(options = {})
  raise ArgumentError unless options.is_a?(Hash)
  merged_options = preset_options.merge(options)
  @size              = merged_options[:size]
  @aspect            = merged_options[:aspect]
  @video_bitrate     = merged_options[:video_bitrate]
  @audio_bitrate     = merged_options[:audio_bitrate]
  @audio_sample_rate = merged_options[:audio_sample_rate]
  @other_options     = merged_options[:other_options]
end

Public Instance Methods

acodec() click to toggle source
# File lib/active_job/ffmpeg/encoder/base.rb, line 35
def acodec
  raise NotImplementedError
end
aspect(filename = nil) click to toggle source
# File lib/active_job/ffmpeg/encoder/base.rb, line 39
def aspect(filename = nil)
  @aspect || Ffmpeg.get_aspect(filename)
end
do_encode(input, output) click to toggle source
# File lib/active_job/ffmpeg/encoder/base.rb, line 53
def do_encode(input, output)
  @input_filename = input
  @output_filename = output
  cmd = %W(#{Ffmpeg.ffmpeg_cmd} -y -i #{@input_filename} -f #{format} -s #{size} -aspect #{aspect(@input_filename)} -vcodec #{vcodec} -b:v #{video_bitrate} -acodec #{acodec} -ar #{audio_sample_rate} -b:a #{audio_bitrate})
  cmd.concat(other_options.split(" "))
  cmd.reject!(&:empty?)
  cmd << @output_filename

  if ENV["DEBUG"]
    Ffmpeg.logger.debug(cmd.map(&:shellescape).join(" "))
  end

  duration = nil
  time = nil
  progress = nil

  Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_th|
    while line = stderr.gets("\r")
      if line =~ /Duration:(\s.?(\d*):(\d*):(\d*)\.(\d*))/
        duration = $2.to_i * 3600 + $3.to_i * 60 + $4.to_i
      end

      if line =~ /frame=.*time=(\s*(\d*):(\d*):(\d*)\.(\d*))/
        time = $2.to_i * 3600 + $3.to_i * 60 + $4.to_i
      end

      if duration && time
        progress = (time / duration.to_f)
        on_progress.call(progress) if on_progress
      end
    end

    wait_th.join
  end

  on_complete.call(self) if on_complete
end
format() click to toggle source
# File lib/active_job/ffmpeg/encoder/base.rb, line 27
def format
  raise NotImplementedError
end
on_complete=(complete_proc) click to toggle source
# File lib/active_job/ffmpeg/encoder/base.rb, line 48
def on_complete=(complete_proc)
  raise ArgumentError unless complete_proc.is_a?(Proc)
  @on_complete = complete_proc
end
on_progress=(progress_proc) click to toggle source
# File lib/active_job/ffmpeg/encoder/base.rb, line 43
def on_progress=(progress_proc)
  raise ArgumentError unless progress_proc.is_a?(Proc)
  @on_progress = progress_proc
end
preset_options() click to toggle source
# File lib/active_job/ffmpeg/encoder/base.rb, line 23
def preset_options
  {}
end
vcodec() click to toggle source
# File lib/active_job/ffmpeg/encoder/base.rb, line 31
def vcodec
  raise NotImplementedError
end