diff --git a/lib/paperclip/paperclip_processors/transcoder.rb b/lib/paperclip/paperclip_processors/transcoder.rb index d67efea..dd1c619 100755 --- a/lib/paperclip/paperclip_processors/transcoder.rb +++ b/lib/paperclip/paperclip_processors/transcoder.rb @@ -18,6 +18,7 @@ def initialize file, options = {}, attachment = nil @convert_options = set_convert_options(options) @format = options[:format] + @width = options[:width] @geometry = options[:geometry] unless @geometry.nil? @@ -36,6 +37,13 @@ def initialize file, options = {}, attachment = nil @convert_options[:output][:s] = format_geometry(@geometry) if @geometry.present? + # add support for cropping the video if model supports it + target = attachment.instance + if target.respond_to?(:cropping?) && target.cropping? + crop_option = "crop=#{round_even(target.crop_w)}:#{round_even(target.crop_h)}:#{round_even(target.crop_x)}:#{round_even(target.crop_y)}" + @convert_options[:output]["filter:v"] = crop_option + end + attachment.instance_write(:meta, @meta) if attachment end @@ -67,7 +75,17 @@ def make end if @convert_options[:output] @convert_options[:output].each do |h| - @cli.add_output_param h + if h[0] == :vf + if @meta[:width] < @width + #modify the scale on smaller images to ensure it's divisible + #by two + h[1] = "\"scale=trunc(iw/2)*2:trunc(ih/2)*2\"" + end + + @cli.add_output_param h + else + @cli.add_output_param h + end end end end @@ -105,6 +123,13 @@ def format_geometry geometry def output_is_image? !!@format.to_s.match(/jpe?g|png|gif$/) end + + def round_even(num) + num = num.to_i + return 0 if num.zero? + + (num / 2).round(0) * 2 + end end class Attachment diff --git a/paperclip-av-transcoder.gemspec b/paperclip-av-transcoder.gemspec index c62aa5c..0c940f8 100644 --- a/paperclip-av-transcoder.gemspec +++ b/paperclip-av-transcoder.gemspec @@ -25,6 +25,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency "sqlite3" spec.add_development_dependency "coveralls" - spec.add_dependency "paperclip", ">=2.5.2" + spec.add_dependency "kt-paperclip", ">=2.5.2" spec.add_dependency "av", "~> 0.9.0" end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1735ebf..cff04ba 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -34,11 +34,13 @@ class Document < ActiveRecord::Base styles: { small: { format: 'ogv', + width: 320, convert_options: { output: { ab: '256k', ar: 44100, - ac: 2 + ac: 2, + vf: "scale=320:-2" } } }, @@ -61,4 +63,4 @@ class Document < ActiveRecord::Base do_not_validate_attachment_file_type :video do_not_validate_attachment_file_type :image -end \ No newline at end of file +end