Class: RVideo::Tools::Ffmpeg

Inherits:
Object
  • Object
show all
Includes:
AbstractTool::InstanceMethods
Defined in:
lib/rvideo/tools/ffmpeg.rb

Constant Summary collapse

RESOLUTION_ABBREVIATIONS =
{
  "sqcif"  => "128x96",
  "qcif"   => "176x144",
  "cif"    => "352x288",
  "4cif"   => "704x576",
  "qqvga"  => "160x120",
  "qvga"   => "320x240",
  "vga"    => "640x480",
  "svga"   => "800x600",
  "xga"    => "1024x768",
  "uxga"   => "1600x1200",
  "qxga"   => "2048x1536",
  "sxga"   => "1280x1024",
  "qsxga"  => "2560x2048",
  "hsxga"  => "5120x4096",
  "wvga"   => "852x480",
  "wxga"   => "1366x768",
  "wsxga"  => "1600x1024",
  "wuxga"  => "1920x1200",
  "woxga"  => "2560x1600",
  "wqsxga" => "3200x2048",
  "wquxga" => "3840x2400",
  "whsxga" => "6400x4096",
  "whuxga" => "7680x4800",
  "cga"    => "320x200",
  "ega"    => "640x350",
  "hd480"  => "852x480",
  "hd720"  => "1280x720",
  "hd1080" => "1920x1080"
}
VALID_ASPECT_STRINGS =
["4:3", "16:9"]
VALID_ASPECT_FLOATS =
[1.3333, 1.7777]
DEFAULT_VIDEO_BIT_RATE_PARAMETER =

The flag used to set video bitrate has apparently changed between different ffmpeg versions. In the latest builds -b is used. In older builds it was -v which is now used to set verbosity of logging.

"b"

Instance Attribute Summary collapse

Attributes included from AbstractTool::InstanceMethods

#command, #options, #progress, #raw_result

Instance Method Summary collapse

Methods included from AbstractTool::InstanceMethods

abstract_attribute_formatter, #audio_bit_rate, #audio_channels, #audio_sample_rate, #calculate_height, #calculate_width, #deinterlace, #do_execute, #execute, #fps, #get_audio_bit_rate, #get_audio_channels, #get_audio_sample_rate, #get_constrain_resolution, #get_deinterlace, #get_fit_to_height_resolution, #get_fit_to_width_resolution, #get_fps, #get_letterbox_resolution, #get_mono_audio, #get_original_fps, #get_original_resolution, #get_padding_resolution, #get_resolution, #get_specific_audio_bit_rate, #get_specific_audio_sample_rate, #get_specific_fps, #get_specific_resolution, #get_stereo_audio, #get_valid_height, #get_valid_width, #get_video_bit_rate, #get_video_bit_rate_max, #get_video_bit_rate_min, #get_video_bit_rate_tolerance, #get_video_quality, #original, #original=, #resolution, #resolution_and_padding, #resolution_keep_aspect_ratio, #temp_dir, #valid_dimension?, #video_bit_rate, #video_bit_rate_max, #video_bit_rate_min, #video_bit_rate_tolerance, #video_quality

Constructor Details

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

Returns a new instance of Ffmpeg.



50
51
52
53
54
# File 'lib/rvideo/tools/ffmpeg.rb', line 50

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

Instance Attribute Details

#audio_sizeObject (readonly)

Returns the value of attribute audio_size.



48
49
50
# File 'lib/rvideo/tools/ffmpeg.rb', line 48

def audio_size
  @audio_size
end

#frameObject (readonly)

Returns the value of attribute frame.



48
49
50
# File 'lib/rvideo/tools/ffmpeg.rb', line 48

def frame
  @frame
end

#header_sizeObject (readonly)

Returns the value of attribute header_size.



48
49
50
# File 'lib/rvideo/tools/ffmpeg.rb', line 48

def header_size
  @header_size
end

#output_bitrateObject (readonly)

Returns the value of attribute output_bitrate.



48
49
50
# File 'lib/rvideo/tools/ffmpeg.rb', line 48

def output_bitrate
  @output_bitrate
end

#output_fpsObject (readonly)

Returns the value of attribute output_fps.



48
49
50
# File 'lib/rvideo/tools/ffmpeg.rb', line 48

def output_fps
  @output_fps
end

#overheadObject (readonly)

Returns the value of attribute overhead.



48
49
50
# File 'lib/rvideo/tools/ffmpeg.rb', line 48

def overhead
  @overhead
end

#pidObject (readonly)

Returns the value of attribute pid.



48
49
50
# File 'lib/rvideo/tools/ffmpeg.rb', line 48

def pid
  @pid
end

#psnrObject (readonly)

Returns the value of attribute psnr.



48
49
50
# File 'lib/rvideo/tools/ffmpeg.rb', line 48

def psnr
  @psnr
end

#qObject (readonly)

Returns the value of attribute q.



48
49
50
# File 'lib/rvideo/tools/ffmpeg.rb', line 48

def q
  @q
end

#sizeObject (readonly)

Returns the value of attribute size.



48
49
50
# File 'lib/rvideo/tools/ffmpeg.rb', line 48

def size
  @size
end

#timeObject (readonly)

Returns the value of attribute time.



48
49
50
# File 'lib/rvideo/tools/ffmpeg.rb', line 48

def time
  @time
end

#video_sizeObject (readonly)

Returns the value of attribute video_size.



48
49
50
# File 'lib/rvideo/tools/ffmpeg.rb', line 48

def video_size
  @video_size
end

Instance Method Details

#do_execute_with_progress(command, &block) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/rvideo/tools/ffmpeg.rb', line 154

def do_execute_with_progress(command,&block)
  @raw_result = ''
  duration = 0
  CommandExecutor::execute_with_block(command, "\r") do |line|
    progress, duration = parse_progress(line, duration)
    block.call(progress) if block && progress
    @raw_result += line.to_s + "\r"
  end
end

#execute_with_progress(&block) ⇒ Object



164
165
166
167
168
169
# File 'lib/rvideo/tools/ffmpeg.rb', line 164

def execute_with_progress(&block)
  RVideo.logger.info("\nExecuting Command: #{@command}\n")
  do_execute_with_progress(@command, &block)
rescue RVideo::CommandExecutor::ProcessHungError
  raise TranscoderError, "Transcoder hung."
end

#format_audio_bit_rate(params = {}) ⇒ Object



146
147
148
# File 'lib/rvideo/tools/ffmpeg.rb', line 146

def format_audio_bit_rate(params={})
  "-ab #{params[:bit_rate]}k"
end

#format_audio_channels(params = {}) ⇒ Object



142
143
144
# File 'lib/rvideo/tools/ffmpeg.rb', line 142

def format_audio_channels(params={})
  "-ac #{params[:channels]}"
end

#format_audio_sample_rate(params = {}) ⇒ Object



150
151
152
# File 'lib/rvideo/tools/ffmpeg.rb', line 150

def format_audio_sample_rate(params={})
  "-ar #{params[:sample_rate]}"
end

#format_deinterlace(params = {}) ⇒ Object



61
62
63
# File 'lib/rvideo/tools/ffmpeg.rb', line 61

def format_deinterlace(params={})
  params[:deinterlace] ? "-deinterlace" : ""
end

#format_filter_pad(params = {}) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/rvideo/tools/ffmpeg.rb', line 131

def format_filter_pad(params={})
  plr = ((params[:letterbox][:width] - params[:scale][:width]) / 2).to_i
  ptb = ((params[:letterbox][:height] - params[:scale][:height]) / 2).to_i
  width = params[:letterbox][:width]
  heigth = params[:letterbox][:height]
  
  if(plr > 0 || ptb > 0)
    "pad=#{width.to_i}:#{heigth.to_i}:#{plr.to_i}:#{ptb.to_i}"
  end
end

#format_filter_rotation(params = {}) ⇒ Object



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

def format_filter_rotation(params={})
  "transpose=#{params[:rotate].to_i / 90}"
end

#format_filter_scale(params = {}) ⇒ Object



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

def format_filter_scale(params={})
  "scale=#{params[:scale][:width].to_i}:#{params[:scale][:height].to_i}"
end

#format_fps(params = {}) ⇒ Object



65
66
67
# File 'lib/rvideo/tools/ffmpeg.rb', line 65

def format_fps(params={})
  "-r #{params[:fps]}"
end

#format_resolution(params = {}) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rvideo/tools/ffmpeg.rb', line 108

def format_resolution(params={})
  p = []

  if original.rotated?
    params[:rotate] = original.video_orientation
    p << format_filter_rotation(params)
  end
  
  p << format_filter_scale(params)
  p << format_filter_pad(params) if params.has_key?(:letterbox)
  p.compact!
  
  %(-vf '#{p.join(',')}')
end

#format_video_bit_rate(params = {}) ⇒ Object



69
70
71
# File 'lib/rvideo/tools/ffmpeg.rb', line 69

def format_video_bit_rate(params = {})
  "-#{video_bit_rate_parameter} #{params[:video_bit_rate]}k"
end

#format_video_bit_rate_max(params = {}) ⇒ Object



81
82
83
# File 'lib/rvideo/tools/ffmpeg.rb', line 81

def format_video_bit_rate_max(params = {})
  "-maxrate #{params[:video_bit_rate_max]}k"
end

#format_video_bit_rate_min(params = {}) ⇒ Object



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

def format_video_bit_rate_min(params = {})
  "-minrate #{params[:video_bit_rate_min]}k"
end

#format_video_bit_rate_tolerance(params = {}) ⇒ Object



73
74
75
# File 'lib/rvideo/tools/ffmpeg.rb', line 73

def format_video_bit_rate_tolerance(params = {})
  "-bt #{params[:video_bit_rate_tolerance]}k"
end

#format_video_quality(params = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rvideo/tools/ffmpeg.rb', line 85

def format_video_quality(params={})
  bitrate = params[:video_bit_rate].blank? ? nil : params[:video_bit_rate]
  
  params.merge! get_original_fps unless params[:fps]
  
  factor  = params[:scale][:width].to_f * params[:scale][:height].to_f * params[:fps].to_f
  
  case params[:video_quality]
  when 'low'
    bitrate ||= (factor / 12000).to_i
    params[:video_bit_rate] = bitrate
    "#{format_video_bit_rate params} -crf 30 -me zero -subq 1 -refs 1 -threads auto"
  when 'medium'
    bitrate ||= (factor / 9000).to_i
    params[:video_bit_rate] = bitrate
    "#{format_video_bit_rate params} -crf 22 -flags +loop -cmp +sad -partitions +parti4x4+partp8x8+partb8x8 -flags2 +mixed_refs -me hex -subq 3 -trellis 1 -refs 2 -bf 3 -b_strategy 1 -coder 1 -me_range 16 -g 250"
  when 'high'
    bitrate ||= (factor / 3600).to_i
    params[:video_bit_rate] = bitrate
    "#{format_video_bit_rate params} -crf 18 -flags +loop -cmp +sad -partitions +parti4x4+partp8x8+partb8x8 -flags2 +mixed_refs -me full -subq 6 -trellis 1 -refs 3 -bf 3 -b_strategy 1 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71"
  end
end

#tool_commandObject

Not sure if this is needed anymore…



57
58
59
# File 'lib/rvideo/tools/ffmpeg.rb', line 57

def tool_command
  'ffmpeg'
end