Class: Paperclip::Ffmpeg
- Inherits:
-
Processor
- Object
- Processor
- Paperclip::Ffmpeg
- Defined in:
- lib/paperclip_processors/ffmpeg.rb
Instance Attribute Summary collapse
-
#convert_options ⇒ Object
Returns the value of attribute convert_options.
-
#format ⇒ Object
Returns the value of attribute format.
-
#geometry ⇒ Object
Returns the value of attribute geometry.
-
#whiny ⇒ Object
Returns the value of attribute whiny.
Class Method Summary collapse
- .detect_command(command) ⇒ Object
- .detect_ffmpeg_or_avconv ⇒ Object
- .detect_ffprobe_or_avprobe ⇒ Object
- .log(message) ⇒ Object
Instance Method Summary collapse
- #identify ⇒ Object
-
#initialize(file, options = {}, attachment = nil) ⇒ Ffmpeg
constructor
Creates a Video object set to work on the
filegiven. -
#make ⇒ Object
Performs the transcoding of the
fileinto a thumbnail/video.
Constructor Details
#initialize(file, options = {}, attachment = nil) ⇒ Ffmpeg
Creates a Video object set to work on the file given. It will attempt to transcode the video into one defined by target_geometry which is a “WxH”-style string. format should be specified. Video transcoding will raise no errors unless whiny is true (which it is, by default. If convert_options is set, the options will be appended to the convert command upon video transcoding.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/paperclip_processors/ffmpeg.rb', line 11 def initialize file, = {}, = nil = { :input => {}, :output => { :y => nil } } unless [:convert_options].nil? || [:convert_options].class != Hash unless [:convert_options][:input].nil? || [:convert_options][:input].class != Hash [:input].reverse_merge! [:convert_options][:input] end unless [:convert_options][:output].nil? || [:convert_options][:output].class != Hash [:output].reverse_merge! [:convert_options][:output] end end @geometry = [:geometry] @file = file @keep_aspect = !@geometry.nil? && @geometry[-1,1] != '!' @pad_only = @keep_aspect && @geometry[-1,1] == '#' @enlarge_only = @keep_aspect && @geometry[-1,1] == '<' @shrink_only = @keep_aspect && @geometry[-1,1] == '>' @whiny = [:whiny].nil? ? true : [:whiny] @format = [:format] @time = [:time].nil? ? 3 : [:time] @current_format = File.extname(@file.path) @basename = File.basename(@file.path, @current_format) = identify @pad_color = [:pad_color].nil? ? "black" : [:pad_color] @auto_rotate = [:auto_rotate].nil? ? false : [:auto_rotate] .instance_write(:meta, ) end |
Instance Attribute Details
#convert_options ⇒ Object
Returns the value of attribute convert_options.
3 4 5 |
# File 'lib/paperclip_processors/ffmpeg.rb', line 3 def end |
#format ⇒ Object
Returns the value of attribute format.
3 4 5 |
# File 'lib/paperclip_processors/ffmpeg.rb', line 3 def format @format end |
#geometry ⇒ Object
Returns the value of attribute geometry.
3 4 5 |
# File 'lib/paperclip_processors/ffmpeg.rb', line 3 def geometry @geometry end |
#whiny ⇒ Object
Returns the value of attribute whiny.
3 4 5 |
# File 'lib/paperclip_processors/ffmpeg.rb', line 3 def whiny @whiny end |
Class Method Details
.detect_command(command) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/paperclip_processors/ffmpeg.rb', line 246 def self.detect_command(command) command = "if command -v #{command} 2>/dev/null; then echo \"true\"; else echo \"false\"; fi" Ffmpeg.log(command) if @whiny result = Cocaine::CommandLine.new(command).run Ffmpeg.log("Result of command: #{result}") if @whiny case result when /true/ return true when /false/ return false else return nil end end |
.detect_ffmpeg_or_avconv ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/paperclip_processors/ffmpeg.rb', line 216 def self.detect_ffmpeg_or_avconv # Check whether ffmpeg or avconv is installed result = Ffmpeg.detect_command("ffmpeg") Ffmpeg.log("Result of command: #{result}") if @whiny if result == true Ffmpeg.log("Result of command: #{"ffmpeg"}") if @whiny return "ffmpeg" elsif result == false Ffmpeg.log("Result of command: #{"avconv"}") if @whiny return "avconv" else return "Error: no video conversion library detected. Please install ffmpeg or avconv." end end |
.detect_ffprobe_or_avprobe ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/paperclip_processors/ffmpeg.rb', line 231 def self.detect_ffprobe_or_avprobe # Check whether ffprobe or avprobe is installed result = Ffmpeg.detect_command("ffprobe") Ffmpeg.log("Result of command: #{result}") if @whiny if result == true Ffmpeg.log("Result of command: #{"ffprobe"}") if @whiny return "ffprobe" elsif result == false Ffmpeg.log("Result of command: #{"avprobe"}") if @whiny return "avprobe" else return "Error: no video conversion library detected. Please install ffmpeg or avconv." end end |
.log(message) ⇒ Object
212 213 214 |
# File 'lib/paperclip_processors/ffmpeg.rb', line 212 def self.log Paperclip.log "[ffmpeg] #{message}" end |
Instance Method Details
#identify ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/paperclip_processors/ffmpeg.rb', line 181 def identify = {} av_lib_version = Ffmpeg.detect_ffprobe_or_avprobe command = "#{av_lib_version} \"#{File.expand_path(@file.path)}\" 2>&1" Paperclip.log("[ffmpeg] #{command}") ffmpeg = Cocaine::CommandLine.new(command).run ffmpeg.split("\n").each do |line| if line =~ /(([\d\.]*)\s.?)fps,/ [:fps] = $1.to_i end # Matching lines like: # Video: h264, yuvj420p, 640x480 [PAR 72:72 DAR 4:3], 10301 kb/s, 30 fps, 30 tbr, 600 tbn, 600 tbc if line =~ /Video:(.*)/ v = $1.to_s size = v.match(/\d{3,5}x\d{3,5}/).to_s [:size] = size [:aspect] = size.split('x').first.to_f / size.split('x').last.to_f end # Matching Duration: 00:01:31.66, start: 0.000000, bitrate: 10404 kb/s if line =~ /Duration:(\s.?(\d*):(\d*):(\d*\.\d*))/ [:length] = $2.to_s + ":" + $3.to_s + ":" + $4.to_s end if line =~ /rotate\s*:\s(\d*)/ [:rotate] = $1.to_i end end Paperclip.log("[ffmpeg] Command Success") if @whiny end |
#make ⇒ Object
Performs the transcoding of the file into a thumbnail/video. Returns the Tempfile that contains the new image/video.
43 44 45 46 47 48 49 50 51 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/paperclip_processors/ffmpeg.rb', line 43 def make Ffmpeg.log("Making...") if @whiny src = @file Ffmpeg.log("Building Destination File: '#{@basename}' + '#{@format}'") if @whiny dst = Tempfile.new([@basename, @format ? ".#{@format}" : '']) Ffmpeg.log("Destination File Built") if @whiny dst.binmode parameters = [] Ffmpeg.log("Adding Geometry") if @whiny # Add geometry if @geometry Ffmpeg.log("Extracting Target Dimensions") if @whiny # Extract target dimensions if @geometry =~ /(\d*)x(\d*)/ target_width = $1 target_height = $2 end # Only calculate target dimensions if we have current dimensions unless [:size].nil? Ffmpeg.log("Target Size is Available") if @whiny current_width, current_height = [:size].split('x') # Current width and height if @keep_aspect Ffmpeg.log("Keeping Aspect Ratio") if @whiny if @enlarge_only Ffmpeg.log("Enlarge Only") if @whiny if current_width.to_i < target_width.to_i # Keep aspect ratio width = target_width.to_i height = (width.to_f / ([:aspect].to_f)).to_i [:output][:s] = "#{width.to_i/2*2}x#{height.to_i/2*2}" Ffmpeg.log("Convert Options: #{@convert_options[:output][:s]}") if @whiny else Ffmpeg.log("Source is Larger than Destination, Doing Nothing") if @whiny #return nil end elsif @shrink_only Ffmpeg.log("Shrink Only") if @whiny if current_width.to_i > target_width.to_i # Keep aspect ratio width = target_width.to_i height = (width.to_f / ([:aspect].to_f)).to_i [:output][:s] = "#{width.to_i/2*2}x#{height.to_i/2*2}" Ffmpeg.log("Convert Options: #{@convert_options[:output][:s]}") if @whiny else Ffmpeg.log("Source is Smaller than Destination, Doing Nothing") if @whiny #return nil end elsif @pad_only Ffmpeg.log("Pad Only") if @whiny # Keep aspect ratio width = target_width.to_i height = (width.to_f / ([:aspect].to_f)).to_i # We should add half the delta as a padding offset Y pad_y = (target_height.to_f - height.to_f) / 2 # There could be options already set [:output][:vf][/\A/] = ',' if [:output][:vf] [:output][:vf] ||= '' if pad_y > 0 [:output][:vf][/\A/] = "scale=#{width}:-1,pad=#{width.to_i}:#{target_height.to_i}:0:#{pad_y}:#@pad_color" else [:output][:vf][/\A/] = "scale=#{width}:-1,crop=#{width.to_i}:#{height.to_i}" end Ffmpeg.log("Convert Options: #{@convert_options[:output][:s]}") if @whiny else Ffmpeg.log("Resize") if @whiny # Keep aspect ratio width = target_width.to_i height = (width.to_f / ([:aspect].to_f)).to_i [:output][:s] = "#{width.to_i/2*2}x#{height.to_i/2*2}" Ffmpeg.log("Convert Options: #{@convert_options[:output][:s]}") if @whiny end else Ffmpeg.log("Not Keeping Aspect Ratio") if @whiny # Do not keep aspect ratio [:output][:s] = "#{target_width.to_i/2*2}x#{target_height.to_i/2*2}" Ffmpeg.log("Convert Options: #{@convert_options[:output][:s]}") if @whiny end end end # If file has rotation, rotate it back to horizontal if @auto_rotate && ![:rotate].nil? Ffmpeg.log("Adding rotation #{@meta[:rotate]}") if @whiny case [:rotate] when 90 [:output][:vf] = 'transpose=1' when 180 [:output][:vf] = 'vflip, hflip' when 270 [:output][:vf] = 'transpose=2' end end Ffmpeg.log("Adding Format") if @whiny # Add format case @format when 'jpg', 'jpeg', 'png', 'gif' # Images [:input][:ss] = @time [:output][:vframes] = 1 [:output][:f] = 'image2' when 'webm' # WebM [:output][:acodec] = 'libvorbis' [:output][:vcodec] = 'libvpx' [:output][:f] = 'webm' when 'ogv' # Ogg Theora [:output][:acodec] = 'libvorbis' [:output][:vcodec] = 'libtheora' [:output][:f] = 'ogg' when 'mp4' [:output][:acodec] = 'aac' [:output][:strict] = 'experimental' end Ffmpeg.log("Adding Source") if @whiny # Add source # Validations on the values. These could be either nil. parameters << [:input].map { |k,v| "-#{k.to_s} #{v} " if !v.nil? && (v.is_a?(Numeric) || !v.empty?) } parameters << "-i :source" parameters << [:output].map { |k,v| "-#{k.to_s} #{v} " if !v.nil? && (v.is_a?(Numeric) || !v.empty?) } parameters << "-y :dest" Ffmpeg.log("Building Parameters") if @whiny parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ") Ffmpeg.log(parameters) begin av_lib_version = Ffmpeg.detect_ffmpeg_or_avconv success = Paperclip.run(av_lib_version, parameters, :source => "#{File.expand_path(src.path)}", :dest => File.(dst.path)) rescue Cocaine::ExitStatusError => e raise Paperclip::Error, "error while processing video for #{@basename}: #{e}" if @whiny end dst end |