From dea65dc5e094ac765e0bab9826312f2059847896 Mon Sep 17 00:00:00 2001 From: Justin Cooper Date: Fri, 20 Nov 2015 12:11:37 -0600 Subject: [PATCH 1/5] add width calculations based on size to prevent upscaling of videos by excluding vf flag if necessary --- lib/paperclip/paperclip_processors/transcoder.rb | 7 ++++++- spec/spec_helper.rb | 6 ++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/paperclip/paperclip_processors/transcoder.rb b/lib/paperclip/paperclip_processors/transcoder.rb index 36b3f88..af9836f 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? @@ -66,7 +67,11 @@ def make end if @convert_options[:output] @convert_options[:output].each do |h| - @cli.add_output_param h + if h[0] == :vf + @cli.add_output_param h if @meta[:width] >= @width + else + @cli.add_output_param h + end end end 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 From 168004c861a1634d043fbd123ff5c6a13e4713a3 Mon Sep 17 00:00:00 2001 From: Justin Cooper Date: Mon, 23 Nov 2015 14:04:55 -0600 Subject: [PATCH 2/5] scale so all sizes are divisible by 2 --- lib/paperclip/paperclip_processors/transcoder.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/paperclip/paperclip_processors/transcoder.rb b/lib/paperclip/paperclip_processors/transcoder.rb index af9836f..9faeee9 100755 --- a/lib/paperclip/paperclip_processors/transcoder.rb +++ b/lib/paperclip/paperclip_processors/transcoder.rb @@ -68,7 +68,13 @@ def make if @convert_options[:output] @convert_options[:output].each do |h| if h[0] == :vf - @cli.add_output_param h if @meta[:width] >= @width + 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 From 4cfb858521c233c65442ceacc2b3fda0fe66a7e4 Mon Sep 17 00:00:00 2001 From: Justin Cooper Date: Wed, 5 Jul 2017 17:06:12 -0500 Subject: [PATCH 3/5] add support for cropping of videos --- lib/paperclip/paperclip_processors/transcoder.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/paperclip/paperclip_processors/transcoder.rb b/lib/paperclip/paperclip_processors/transcoder.rb index 9faeee9..dd3f35f 100755 --- a/lib/paperclip/paperclip_processors/transcoder.rb +++ b/lib/paperclip/paperclip_processors/transcoder.rb @@ -37,6 +37,12 @@ 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? + @convert_options[:output]["filter:v"] = "crop=#{target.crop_w}:#{target.crop_h}:#{target.crop_x}:#{target.crop_y}" + end + attachment.instance_write(:meta, @meta) if attachment end From 95c13fde3fa5c91eb7575d8574f853b78b99bef5 Mon Sep 17 00:00:00 2001 From: Justin Cooper Date: Thu, 8 Mar 2018 13:54:10 -0600 Subject: [PATCH 4/5] fix divide by 2 error with cropping gifs --- lib/paperclip/paperclip_processors/transcoder.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/paperclip/paperclip_processors/transcoder.rb b/lib/paperclip/paperclip_processors/transcoder.rb index dd3f35f..3574ff2 100755 --- a/lib/paperclip/paperclip_processors/transcoder.rb +++ b/lib/paperclip/paperclip_processors/transcoder.rb @@ -40,7 +40,8 @@ def initialize file, options = {}, attachment = nil # add support for cropping the video if model supports it target = attachment.instance if target.respond_to?(:cropping?) && target.cropping? - @convert_options[:output]["filter:v"] = "crop=#{target.crop_w}:#{target.crop_h}:#{target.crop_x}:#{target.crop_y}" + 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 @@ -121,6 +122,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 From 6f5afa3238b9e2f6cb31549af88b78eb2f846f7d Mon Sep 17 00:00:00 2001 From: Justin Cooper Date: Fri, 18 Dec 2020 12:20:33 -0600 Subject: [PATCH 5/5] set kt-paperclip as dependency --- paperclip-av-transcoder.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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