Class: ActiveJob::Ffmpeg::Encoder::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_job/ffmpeg/encoder/base.rb

Direct Known Subclasses

MP4, WebM

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

#audio_bitrateObject (readonly)

Returns the value of attribute audio_bitrate.



8
9
10
# File 'lib/active_job/ffmpeg/encoder/base.rb', line 8

def audio_bitrate
  @audio_bitrate
end

#audio_sample_rateObject (readonly)

Returns the value of attribute audio_sample_rate.



8
9
10
# File 'lib/active_job/ffmpeg/encoder/base.rb', line 8

def audio_sample_rate
  @audio_sample_rate
end

#input_filenameObject (readonly)

Returns the value of attribute input_filename.



9
10
11
# File 'lib/active_job/ffmpeg/encoder/base.rb', line 9

def input_filename
  @input_filename
end

#on_completeObject

Returns the value of attribute on_complete.



10
11
12
# File 'lib/active_job/ffmpeg/encoder/base.rb', line 10

def on_complete
  @on_complete
end

#on_progressObject

Returns the value of attribute on_progress.



10
11
12
# File 'lib/active_job/ffmpeg/encoder/base.rb', line 10

def on_progress
  @on_progress
end

#other_optionsObject (readonly)

Returns the value of attribute other_options.



8
9
10
# File 'lib/active_job/ffmpeg/encoder/base.rb', line 8

def other_options
  @other_options
end

#output_filenameObject (readonly)

Returns the value of attribute output_filename.



9
10
11
# File 'lib/active_job/ffmpeg/encoder/base.rb', line 9

def output_filename
  @output_filename
end

#sizeObject (readonly)

Returns the value of attribute size.



8
9
10
# File 'lib/active_job/ffmpeg/encoder/base.rb', line 8

def size
  @size
end

#video_bitrateObject (readonly)

Returns the value of attribute video_bitrate.



8
9
10
# File 'lib/active_job/ffmpeg/encoder/base.rb', line 8

def video_bitrate
  @video_bitrate
end

Instance Method Details

#acodecObject

Raises:

  • (NotImplementedError)


35
36
37
# File 'lib/active_job/ffmpeg/encoder/base.rb', line 35

def acodec
  raise NotImplementedError
end

#aspect(filename = nil) ⇒ Object



39
40
41
# File 'lib/active_job/ffmpeg/encoder/base.rb', line 39

def aspect(filename = nil)
  @aspect || Ffmpeg.get_aspect(filename)
end

#do_encode(input, output) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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

#formatObject

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/active_job/ffmpeg/encoder/base.rb', line 27

def format
  raise NotImplementedError
end

#preset_optionsObject



23
24
25
# File 'lib/active_job/ffmpeg/encoder/base.rb', line 23

def preset_options
  {}
end

#vcodecObject

Raises:

  • (NotImplementedError)


31
32
33
# File 'lib/active_job/ffmpeg/encoder/base.rb', line 31

def vcodec
  raise NotImplementedError
end