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



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

def audio_bit_rate
  format_audio_bit_rate(get_audio_bit_rate)
end

#audio_channelsObject

Audio channels



247
248
249
# File 'lib/rvideo/tools/abstract_tool.rb', line 247

def audio_channels
  format_audio_channels(get_audio_channels)
end

#audio_sample_rateObject

Audio sample rate



299
300
301
# File 'lib/rvideo/tools/abstract_tool.rb', line 299

def audio_sample_rate
  format_audio_sample_rate(get_audio_sample_rate)
end

#calculate_height(ow, oh, w) ⇒ Object



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

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



230
231
232
233
# File 'lib/rvideo/tools/abstract_tool.rb', line 230

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



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

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



90
91
92
# File 'lib/rvideo/tools/abstract_tool.rb', line 90

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
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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}"

  # nice the command if option :nice was given
  # accepts a number 1..19 (nice value) or anything evaluating to true
  unless RUBY_PLATFORM.downcase.include?("mswin")
    if @options.has_key? 'nice'
      if (1..19) === @options['nice']
        final_command = "nice -n#{@options['nice']} #{final_command}"
      elsif @options['nice']
        final_command = "nice #{final_command}"
      end
    end
  end

  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



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

def fps
  format_fps(get_fps)
end

#get_audio_bit_rateObject



286
287
288
289
290
291
292
293
294
# File 'lib/rvideo/tools/abstract_tool.rb', line 286

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



251
252
253
254
255
256
257
258
259
260
261
# File 'lib/rvideo/tools/abstract_tool.rb', line 251

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



303
304
305
306
307
308
309
310
311
# File 'lib/rvideo/tools/abstract_tool.rb', line 303

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



139
140
141
# File 'lib/rvideo/tools/abstract_tool.rb', line 139

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

#get_fit_to_height_resolutionObject



179
180
181
182
183
184
185
186
187
188
# File 'lib/rvideo/tools/abstract_tool.rb', line 179

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



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

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



112
113
114
115
116
117
118
119
120
121
# File 'lib/rvideo/tools/abstract_tool.rb', line 112

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



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/rvideo/tools/abstract_tool.rb', line 190

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



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

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

#get_original_fpsObject



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

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

#get_original_resolutionObject



214
215
216
# File 'lib/rvideo/tools/abstract_tool.rb', line 214

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

#get_resolutionObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rvideo/tools/abstract_tool.rb', line 147

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



271
272
273
# File 'lib/rvideo/tools/abstract_tool.rb', line 271

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

#get_specific_audio_sample_rateObject



275
276
277
# File 'lib/rvideo/tools/abstract_tool.rb', line 275

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

#get_specific_fpsObject



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

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

#get_specific_resolutionObject



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/rvideo/tools/abstract_tool.rb', line 218

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



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

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

#get_video_bit_rateObject



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

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

#get_video_bit_rate_maxObject



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

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

#get_video_bit_rate_minObject



349
350
351
# File 'lib/rvideo/tools/abstract_tool.rb', line 349

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

#get_video_bit_rate_toleranceObject



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

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

#get_video_qualityObject



320
321
322
323
324
325
326
327
# File 'lib/rvideo/tools/abstract_tool.rb', line 320

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



143
144
145
# File 'lib/rvideo/tools/abstract_tool.rb', line 143

def resolution
  format_resolution(get_resolution)
end

#temp_dirObject

Magic parameters



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

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

#valid_dimension?(dim) ⇒ Boolean

Returns:

  • (Boolean)


240
241
242
# File 'lib/rvideo/tools/abstract_tool.rb', line 240

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

#video_bit_rateObject



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

def video_bit_rate
  format_video_bit_rate(get_video_bit_rate)
end

#video_bit_rate_maxObject



353
354
355
# File 'lib/rvideo/tools/abstract_tool.rb', line 353

def video_bit_rate_max
  format_video_bit_rate_max(get_video_bit_rate_max)
end

#video_bit_rate_minObject



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

def video_bit_rate_min
  format_video_bit_rate_min(get_video_bit_rate_min)
end

#video_bit_rate_toleranceObject



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

def video_bit_rate_tolerance
  format_video_bit_rate_tolerance(get_video_bit_rate_tolerance)
end

#video_qualityObject

Video quality



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

def video_quality
  format_video_quality(get_video_quality)
end