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

Included in:
Ffmpeg, Ffmpeg2theora, Flvtool2, HandBrakeCli, Lame, Mencoder, Mp4box, Mp4creator, Mplayer, QtFaststart, Segmenter, 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


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

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options


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

def options
  @options
end

#progressObject (readonly)

Returns the value of attribute progress


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

def progress
  @progress
end

#raw_resultObject (readonly)

Returns the value of attribute raw_result


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

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.


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

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


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

def audio_bit_rate
  format_audio_bit_rate(get_audio_bit_rate)
end

#audio_channelsObject

Audio channels


288
289
290
# File 'lib/rvideo/tools/abstract_tool.rb', line 288

def audio_channels
  format_audio_channels(get_audio_channels)
end

#audio_sample_rateObject

Audio sample rate


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

def audio_sample_rate
  format_audio_sample_rate(get_audio_sample_rate)
end

#calculate_height(ow, oh, w) ⇒ Object


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

def calculate_height(ow, oh, w)
  h = (w.to_f / (ow.to_f / oh.to_f)).to_f rescue get_valid_height
  h = get_valid_height if h.nan?
  h = h.ceil
  (h - h % 2)
end

#calculate_width(ow, oh, h) ⇒ Object


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

def calculate_width(ow, oh, h)
  w = ((ow.to_f / oh.to_f) * h.to_f).to_f rescue get_valid_width
  w = get_valid_width if w.nan?
  w = w.ceil
  (w - w % 2)
end

#deinterlaceObject

Resolution


119
120
121
# File 'lib/rvideo/tools/abstract_tool.rb', line 119

def deinterlace
  format_deinterlace(get_deinterlace)
end

#do_execute(command) ⇒ Object


75
76
77
# File 'lib/rvideo/tools/abstract_tool.rb', line 75

def do_execute(command)
  CommandExecutor::execute_tailing_stderr(command, 500)
end

#executeObject


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 54

def execute
  @output_params = {}
  if block_given? and self.respond_to?(:execute_with_progress)
    execute_with_progress do |progress|
      yield progress if progress != @progress
      @progress = progress
    end
    
    if @progress != 100
      @progress = 100
      yield @progress
    end
  else
    RVideo.logger.info("\nExecuting Command: #{@command}\n")
    @raw_result = do_execute(@command)
  end

  RVideo.logger.info("Result: \n#{@raw_result}")
  parse_result(@raw_result)
end

#fpsObject

FPS aka framerate


93
94
95
# File 'lib/rvideo/tools/abstract_tool.rb', line 93

def fps
  format_fps(get_fps)
end

#get_audio_bit_rateObject


327
328
329
330
331
332
333
334
335
# File 'lib/rvideo/tools/abstract_tool.rb', line 327

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


292
293
294
295
296
297
298
299
300
301
302
# File 'lib/rvideo/tools/abstract_tool.rb', line 292

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


344
345
346
347
348
349
350
351
352
# File 'lib/rvideo/tools/abstract_tool.rb', line 344

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

#get_constrain_resolutionObject


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

def get_constrain_resolution
  lw = get_valid_width
  lh = get_valid_height
  
  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 = lw
  h = calculate_height(original.width, original.height, w)

  if h > lh
    h = lh
    w = calculate_width(original.width, original.height, h)
  end

  { :scale => { :width => w,  :height => h  } }
end

#get_deinterlaceObject


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

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

#get_fit_to_height_resolutionObject


211
212
213
214
215
216
217
218
219
220
# File 'lib/rvideo/tools/abstract_tool.rb', line 211

def get_fit_to_height_resolution
  h = get_valid_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


200
201
202
203
204
205
206
207
208
209
# File 'lib/rvideo/tools/abstract_tool.rb', line 200

def get_fit_to_width_resolution
  w = get_valid_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


97
98
99
100
101
102
103
104
105
# File 'lib/rvideo/tools/abstract_tool.rb', line 97

def get_fps
  fps = @options['fps'] || ""
  case fps
  when "copy"
    get_original_fps
  else
    get_specific_fps
  end
end

#get_letterbox_resolutionObject


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/rvideo/tools/abstract_tool.rb', line 222

def get_letterbox_resolution
  lw = get_valid_width
  lh = get_valid_height

  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 = lw
  h = calculate_height(original.width, original.height, w)

  if h > lh
    h = lh
    w = calculate_width(original.width, original.height, h)
  end

  { :scale     => { :width => w,  :height => h  },
    :letterbox => { :width => lw, :height => lh } }
end

#get_mono_audioObject


308
309
310
# File 'lib/rvideo/tools/abstract_tool.rb', line 308

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

#get_original_fpsObject


107
108
109
110
# File 'lib/rvideo/tools/abstract_tool.rb', line 107

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

#get_original_resolutionObject


243
244
245
# File 'lib/rvideo/tools/abstract_tool.rb', line 243

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

#get_padding_resolutionObject


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rvideo/tools/abstract_tool.rb', line 159

def get_padding_resolution
  lw = get_valid_width
  lh = get_valid_height
  
  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 = lw
  h = calculate_height(original.width, original.height, w)
  
  if h > lh
    h = lh
    w = calculate_width(original.width, original.height, h)
  end

  { :scale     => { :width => w,  :height => h  },
    :letterbox => { :width => w, :height => lh } }
end

#get_resolutionObject


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rvideo/tools/abstract_tool.rb', line 139

def get_resolution

  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


312
313
314
# File 'lib/rvideo/tools/abstract_tool.rb', line 312

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

#get_specific_audio_sample_rateObject


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

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

#get_specific_fpsObject


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

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

#get_specific_resolutionObject


247
248
249
250
251
252
253
254
255
256
257
# File 'lib/rvideo/tools/abstract_tool.rb', line 247

def get_specific_resolution
  w = get_valid_width
  h = get_valid_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


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

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

#get_valid_heightObject


281
282
283
# File 'lib/rvideo/tools/abstract_tool.rb', line 281

def get_valid_height
  @options[:height].to_i - (@options[:height].to_i % 2)
end

#get_valid_widthObject


277
278
279
# File 'lib/rvideo/tools/abstract_tool.rb', line 277

def get_valid_width
  @options[:width].to_i - (@options[:width].to_i % 2)
end

#get_video_bit_rateObject


374
375
376
# File 'lib/rvideo/tools/abstract_tool.rb', line 374

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

#get_video_bit_rate_maxObject


398
399
400
# File 'lib/rvideo/tools/abstract_tool.rb', line 398

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

#get_video_bit_rate_minObject


390
391
392
# File 'lib/rvideo/tools/abstract_tool.rb', line 390

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

#get_video_bit_rate_toleranceObject


382
383
384
# File 'lib/rvideo/tools/abstract_tool.rb', line 382

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

#get_video_qualityObject


361
362
363
364
365
366
367
368
# File 'lib/rvideo/tools/abstract_tool.rb', line 361

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


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

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

#originalObject


402
403
404
# File 'lib/rvideo/tools/abstract_tool.rb', line 402

def original
  @original || inspect_original
end

#original=(value) ⇒ Object


406
407
408
# File 'lib/rvideo/tools/abstract_tool.rb', line 406

def original=(value)
  @original = value
end

#resolutionObject


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

def resolution
  format_resolution(get_resolution)
end

#resolution_and_paddingObject


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

def resolution_and_padding
  format_resolution(get_padding_resolution)
end

#resolution_keep_aspect_ratioObject


135
136
137
# File 'lib/rvideo/tools/abstract_tool.rb', line 135

def resolution_keep_aspect_ratio
  format_resolution(get_constrain_resolution)
end

#temp_dirObject

Magic parameters


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

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

#valid_dimension?(dim) ⇒ Boolean

Returns:

  • (Boolean)

273
274
275
# File 'lib/rvideo/tools/abstract_tool.rb', line 273

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

#video_bit_rateObject


370
371
372
# File 'lib/rvideo/tools/abstract_tool.rb', line 370

def video_bit_rate
  format_video_bit_rate(get_video_bit_rate)
end

#video_bit_rate_maxObject


394
395
396
# File 'lib/rvideo/tools/abstract_tool.rb', line 394

def video_bit_rate_max
  format_video_bit_rate_max(get_video_bit_rate_max)
end

#video_bit_rate_minObject


386
387
388
# File 'lib/rvideo/tools/abstract_tool.rb', line 386

def video_bit_rate_min
  format_video_bit_rate_min(get_video_bit_rate_min)
end

#video_bit_rate_toleranceObject


378
379
380
# File 'lib/rvideo/tools/abstract_tool.rb', line 378

def video_bit_rate_tolerance
  format_video_bit_rate_tolerance(get_video_bit_rate_tolerance)
end

#video_qualityObject

Video quality


357
358
359
# File 'lib/rvideo/tools/abstract_tool.rb', line 357

def video_quality
  format_video_quality(get_video_quality)
end