Module: RVideo::Tools::AbstractTool::InstanceMethods

Included in:
Ffmpeg, Ffmpeg2theora, Flvtool2, Mencoder, Mp4box, Mp4creator, Mplayer, QtFaststart, Yamdi
Defined in:
lib/rvideo/tools/abstract_tool.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



43
44
45
# File 'lib/rvideo/tools/abstract_tool.rb', line 43

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



43
44
45
# File 'lib/rvideo/tools/abstract_tool.rb', line 43

def options
  @options
end

#original=(value) ⇒ Object (writeonly)

Sets the attribute original

Parameters:

  • value

    the value to set the attribute original to.



44
45
46
# File 'lib/rvideo/tools/abstract_tool.rb', line 44

def original=(value)
  @original = value
end

#raw_resultObject (readonly)

Returns the value of attribute raw_result.



43
44
45
# File 'lib/rvideo/tools/abstract_tool.rb', line 43

def raw_result
  @raw_result
end

Class Method Details

.abstract_attribute_formatter(*names) ⇒ Object

Defines abstract methods in the convention of “format_#attribute” which are meant to be redefined by classes including this behavior.



25
26
27
28
29
30
31
32
33
34
# File 'lib/rvideo/tools/abstract_tool.rb', line 25

def self.abstract_attribute_formatter(*names)
  names.map { |n| "format_#{n}" }.each do |name|
    class_eval %{
      def #{name}(params = {})
        raise ParameterError,
          "The #{self.class} tool has not implemented the :#{name} method."
      end
    }, __FILE__, __LINE__
  end
end

Instance Method Details

#audio_bit_rateObject

Audio bit rate



269
270
271
# File 'lib/rvideo/tools/abstract_tool.rb', line 269

def audio_bit_rate
  format_audio_bit_rate(get_audio_bit_rate)
end

#audio_channelsObject

Audio channels



234
235
236
# File 'lib/rvideo/tools/abstract_tool.rb', line 234

def audio_channels
  format_audio_channels(get_audio_channels)
end

#audio_sample_rateObject

Audio sample rate



286
287
288
# File 'lib/rvideo/tools/abstract_tool.rb', line 286

def audio_sample_rate
  format_audio_sample_rate(get_audio_sample_rate)
end

#calculate_height(ow, oh, w) ⇒ Object



222
223
224
225
# File 'lib/rvideo/tools/abstract_tool.rb', line 222

def calculate_height(ow, oh, w)
  h = (w.to_f / (ow.to_f / oh.to_f)).to_i
  (h.to_f / 16).round * 16
end

#calculate_width(ow, oh, h) ⇒ Object



217
218
219
220
# File 'lib/rvideo/tools/abstract_tool.rb', line 217

def calculate_width(ow, oh, h)
  w = ((ow.to_f / oh.to_f) * h.to_f).to_i
  (w.to_f / 16).round * 16
end

#deinterlaceObject

Resolution



122
123
124
# File 'lib/rvideo/tools/abstract_tool.rb', line 122

def deinterlace
  format_deinterlace(get_deinterlace)
end

#do_execute(command) ⇒ Object

Wrapper around the system call, for whenever we need to hook on or redefine this without messing with Kernel



77
78
79
# File 'lib/rvideo/tools/abstract_tool.rb', line 77

def do_execute(command)
  system command
end

#executeObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rvideo/tools/abstract_tool.rb', line 52

def execute
  @output_params = {}
      
  # Dump the log output into a temp file
  log_temp_file_name = "/tmp/transcode_output_#{Time.now.to_i}.txt"
    
  final_command = "#{@command} 2>#{log_temp_file_name}"
  RVideo.logger.info("\nExecuting Command: #{final_command}\n")
  do_execute final_command
      
  populate_raw_result(log_temp_file_name)
      
  RVideo.logger.info("Result: \n#{@raw_result}")
  parse_result(@raw_result)
      
  # Cleanup log file
  begin
    File.delete(log_temp_file_name)
  rescue Exception  => e
    RVideo.logger.error("Failed to delete output log file: #{log_temp_file_name}, e=#{e}")
  end
end

#fpsObject

FPS aka framerate



95
96
97
# File 'lib/rvideo/tools/abstract_tool.rb', line 95

def fps
  format_fps(get_fps)
end

#get_audio_bit_rateObject



273
274
275
276
277
278
279
280
281
# File 'lib/rvideo/tools/abstract_tool.rb', line 273

def get_audio_bit_rate
  bit_rate = @options['audio_bit_rate'] || ""
  case bit_rate
  when ""
    {}
  else
    get_specific_audio_bit_rate
  end
end

#get_audio_channelsObject



238
239
240
241
242
243
244
245
246
247
248
# File 'lib/rvideo/tools/abstract_tool.rb', line 238

def get_audio_channels
  channels = @options['audio_channels'] || ""
  case channels
  when "stereo"
    get_stereo_audio
  when "mono"
    get_mono_audio
  else
    {}
  end
end

#get_audio_sample_rateObject



290
291
292
293
294
295
296
297
298
# File 'lib/rvideo/tools/abstract_tool.rb', line 290

def get_audio_sample_rate
  sample_rate = @options['audio_sample_rate'] || ""
  case sample_rate
  when ""
    {}
  else
    get_specific_audio_sample_rate
  end
end

#get_deinterlaceObject



126
127
128
# File 'lib/rvideo/tools/abstract_tool.rb', line 126

def get_deinterlace
  { :deinterlace => @options['deinterlace'] ? true : false }
end

#get_fit_to_height_resolutionObject



166
167
168
169
170
171
172
173
174
175
# File 'lib/rvideo/tools/abstract_tool.rb', line 166

def get_fit_to_height_resolution
  h = @options['height']
      
  raise TranscoderError::ParameterError,
    "invalid height of '#{h}' for fit to height" unless valid_dimension?(h)
      
  w = calculate_width(@original.width, @original.height, h)
      
  { :scale => { :width => w, :height => h } }
end

#get_fit_to_width_resolutionObject



155
156
157
158
159
160
161
162
163
164
# File 'lib/rvideo/tools/abstract_tool.rb', line 155

def get_fit_to_width_resolution
  w = @options['width']
      
  raise TranscoderError::ParameterError,
    "invalid width of '#{w}' for fit to width" unless valid_dimension?(w)
      
  h = calculate_height(@original.width, @original.height, w)
      
  { :scale => { :width => w, :height => h } }
end

#get_fpsObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/rvideo/tools/abstract_tool.rb', line 99

def get_fps
  inspect_original if @original.nil?
  fps = @options['fps'] || ""
  case fps
  when "copy"
    get_original_fps
  else
    get_specific_fps
  end
end

#get_letterbox_resolutionObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/rvideo/tools/abstract_tool.rb', line 177

def get_letterbox_resolution
  lw = @options['width'].to_i
  lh = @options['height'].to_i
      
  raise TranscoderError::ParameterError,
    "invalid width of '#{lw}' for letterbox" unless valid_dimension?(lw)
  raise TranscoderError::ParameterError,
    "invalid height of '#{lh}' for letterbox" unless valid_dimension?(lh)
      
  w = calculate_width(@original.width, @original.height, lh)
  h = calculate_height(@original.width, @original.height, lw)
      
  if w > lw
    w = lw
    h = calculate_height(@original.width, @original.height, lw)
  else
    h = lh
    w = calculate_width(@original.width, @original.height, lh)
  end
      
  { :scale     => { :width => w,  :height => h  },
    :letterbox => { :width => lw, :height => lh } }
end

#get_mono_audioObject



254
255
256
# File 'lib/rvideo/tools/abstract_tool.rb', line 254

def get_mono_audio
  { :channels => "1" }
end

#get_original_fpsObject



110
111
112
113
# File 'lib/rvideo/tools/abstract_tool.rb', line 110

def get_original_fps
  return {} if @original.fps.nil?
  { :fps => @original.fps }
end

#get_original_resolutionObject



201
202
203
# File 'lib/rvideo/tools/abstract_tool.rb', line 201

def get_original_resolution
  { :scale => { :width => @original.width, :height => @original.height } }
end

#get_resolutionObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rvideo/tools/abstract_tool.rb', line 134

def get_resolution
  inspect_original if @original.nil?
      
  case @options['resolution']
  when "copy"      then get_original_resolution
  when "width"     then get_fit_to_width_resolution
  when "height"    then get_fit_to_height_resolution
  when "letterbox" then get_letterbox_resolution
  else
    if @options["width"] and not @options["height"]
      get_fit_to_width_resolution
    elsif @options["height"] and not @options["width"]
      get_fit_to_height_resolution
    elsif @options["width"] and @options["height"]
      get_specific_resolution
    else
      get_original_resolution
    end
  end
end

#get_specific_audio_bit_rateObject



258
259
260
# File 'lib/rvideo/tools/abstract_tool.rb', line 258

def get_specific_audio_bit_rate
  { :bit_rate => @options['audio_bit_rate'] }
end

#get_specific_audio_sample_rateObject



262
263
264
# File 'lib/rvideo/tools/abstract_tool.rb', line 262

def get_specific_audio_sample_rate
  { :sample_rate => @options['audio_sample_rate'] }
end

#get_specific_fpsObject



115
116
117
# File 'lib/rvideo/tools/abstract_tool.rb', line 115

def get_specific_fps
  { :fps => @options['fps'] }
end

#get_specific_resolutionObject



205
206
207
208
209
210
211
212
213
214
215
# File 'lib/rvideo/tools/abstract_tool.rb', line 205

def get_specific_resolution
  w = @options['width']
  h = @options['height']
      
  raise TranscoderError::ParameterError,
    "invalid width of '#{w}' for specific resolution" unless valid_dimension?(w)
  raise TranscoderError::ParameterError,
    "invalid height of '#{h}' for specific resolution" unless valid_dimension?(h)
      
  { :scale => { :width => w, :height => h } }
end

#get_stereo_audioObject



250
251
252
# File 'lib/rvideo/tools/abstract_tool.rb', line 250

def get_stereo_audio
  { :channels => "2" }
end

#get_video_bit_rateObject



320
321
322
# File 'lib/rvideo/tools/abstract_tool.rb', line 320

def get_video_bit_rate
  { :video_bit_rate => @options["video_bit_rate"] }
end

#get_video_bit_rate_maxObject



344
345
346
# File 'lib/rvideo/tools/abstract_tool.rb', line 344

def get_video_bit_rate_max
  { :video_bit_rate_max => @options["video_bit_rate_max"] }
end

#get_video_bit_rate_minObject



336
337
338
# File 'lib/rvideo/tools/abstract_tool.rb', line 336

def get_video_bit_rate_min
  { :video_bit_rate_min => @options["video_bit_rate_min"] }
end

#get_video_bit_rate_toleranceObject



328
329
330
# File 'lib/rvideo/tools/abstract_tool.rb', line 328

def get_video_bit_rate_tolerance
  { :video_bit_rate_tolerance => @options["video_bit_rate_tolerance"] }
end

#get_video_qualityObject



307
308
309
310
311
312
313
314
# File 'lib/rvideo/tools/abstract_tool.rb', line 307

def get_video_quality
  quality = @options['video_quality'] || 'medium'
  
  { :video_quality => quality }.
    merge!(get_fps).
    merge!(get_resolution).
    merge!(get_video_bit_rate)
end

#initialize(raw_command, options = {}) ⇒ Object



46
47
48
49
50
# File 'lib/rvideo/tools/abstract_tool.rb', line 46

def initialize(raw_command, options = {})
  @raw_command = raw_command
  @options = HashWithIndifferentAccess.new(options)
  @command = interpolate_variables(raw_command)
end

#resolutionObject



130
131
132
# File 'lib/rvideo/tools/abstract_tool.rb', line 130

def resolution
  format_resolution(get_resolution)
end

#temp_dirObject

Magic parameters



84
85
86
87
88
89
90
# File 'lib/rvideo/tools/abstract_tool.rb', line 84

def temp_dir
  if @options['output_file']
    "#{File.dirname(@options['output_file'])}/"
  else
    ""
  end
end

#valid_dimension?(dim) ⇒ Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/rvideo/tools/abstract_tool.rb', line 227

def valid_dimension?(dim)
  dim.to_i > 0
end

#video_bit_rateObject



316
317
318
# File 'lib/rvideo/tools/abstract_tool.rb', line 316

def video_bit_rate
  format_video_bit_rate(get_video_bit_rate)
end

#video_bit_rate_maxObject



340
341
342
# File 'lib/rvideo/tools/abstract_tool.rb', line 340

def video_bit_rate_max
  format_video_bit_rate_max(get_video_bit_rate_max)
end

#video_bit_rate_minObject



332
333
334
# File 'lib/rvideo/tools/abstract_tool.rb', line 332

def video_bit_rate_min
  format_video_bit_rate_min(get_video_bit_rate_min)
end

#video_bit_rate_toleranceObject



324
325
326
# File 'lib/rvideo/tools/abstract_tool.rb', line 324

def video_bit_rate_tolerance
  format_video_bit_rate_tolerance(get_video_bit_rate_tolerance)
end

#video_qualityObject

Video quality



303
304
305
# File 'lib/rvideo/tools/abstract_tool.rb', line 303

def video_quality
  format_video_quality(get_video_quality)
end