From 8dfabf91dbb4bbf767d73785263d10177dc08e08 Mon Sep 17 00:00:00 2001 From: Marc Tauber Date: Fri, 7 Oct 2016 19:50:05 +0100 Subject: [PATCH 1/2] Removed redundant geometry modifiers as they don't do anything. Tidied stuff up so I could understand it better. Ignoring geometry if output[:vf] has been specified. If a lambda is passed for any of the input or output options it gets called with meta and options. --- .../paperclip_processors/transcoder.rb | 146 ++++++++---------- 1 file changed, 62 insertions(+), 84 deletions(-) diff --git a/lib/paperclip/paperclip_processors/transcoder.rb b/lib/paperclip/paperclip_processors/transcoder.rb index d67efea..afc407a 100755 --- a/lib/paperclip/paperclip_processors/transcoder.rb +++ b/lib/paperclip/paperclip_processors/transcoder.rb @@ -1,109 +1,86 @@ module Paperclip class Transcoder < Processor - attr_accessor :geometry, :format, :whiny, :convert_options - # 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. - def initialize file, options = {}, attachment = nil - @file = file - @current_format = File.extname(@file.path) - @basename = File.basename(@file.path, @current_format) - @cli = ::Av.cli - @meta = ::Av.cli.identify(@file.path) - @whiny = options[:whiny].nil? ? true : options[:whiny] - - @convert_options = set_convert_options(options) - - @format = options[:format] - - @geometry = options[:geometry] - unless @geometry.nil? - modifier = @geometry[0] - @geometry[0] = '' if ['#', '<', '>'].include? modifier - @width, @height = @geometry.split('x') - @keep_aspect = @width[0] == '!' || @height[0] == '!' - @pad_only = @keep_aspect && modifier == '#' - @enlarge_only = @keep_aspect && modifier == '<' - @shrink_only = @keep_aspect && modifier == '>' - end - @time = options[:time].nil? ? 3 : options[:time] - @auto_rotate = options[:auto_rotate].nil? ? false : options[:auto_rotate] - @pad_color = options[:pad_color].nil? ? "black" : options[:pad_color] + attr_accessor :time, :dst + + attr_reader :basename, :cli, :meta, :whiny, :convert_options, :format, :geometry, :auto_rotate, :path, :current_format + + + def initialize file, options = {}, attachment = nil - @convert_options[:output][:s] = format_geometry(@geometry) if @geometry.present? + @file = file + @path = file.path + @current_format = File.extname(path) + @basename = File.basename(path, current_format) + @cli = ::Av.cli + @meta = ::Av.cli.identify(path) + @whiny = options.fetch(:whiny) { true } + @convert_options = options.fetch(:convert_options) { { output: {} } } + @format = options.fetch(:format) { current_format } + @geometry = options[:geometry] + @time = options.fetch(:time) { 3 } + @auto_rotate = options.fetch(:auto_rotate) { false } + @convert_options[:output][:s] = geometry.gsub(/[#!<>)]/, '') if geometry.present? && convert_options[:output][:vf].blank? + + attachment.instance_write(:meta, meta) if attachment - attachment.instance_write(:meta, @meta) if attachment end - # Performs the transcoding of the +file+ into a thumbnail/video. Returns the Tempfile - # that contains the new image/video. def make ::Av.logger = Paperclip.logger - @cli.add_source @file - dst = Tempfile.new([@basename, @format ? ".#{@format}" : '']) + cli.add_source(file) + self.dst = Tempfile.new([basename, ".#{format}"]) dst.binmode + meta ? transcode : skip_transcode + dst + end - if @meta - log "Transcoding supported file #{@file.path}" - @cli.add_source(@file.path) - @cli.add_destination(dst.path) - @cli.reset_input_filters - - if output_is_image? - @time = @time.call(@meta, @options) if @time.respond_to?(:call) - @cli.filter_seek @time - @cli.filter_rotate @meta[:rotate] if @auto_rotate && !@meta[:rotate].nil? - end + def transcode + log "Transcoding supported file #{path}" + cli.add_source(path) + cli.add_destination(dst.path) + cli.reset_input_filters + configure_image if output_is_image? + transfer_convert_options + run + end - if @convert_options.present? - if @convert_options[:input] - @convert_options[:input].each do |h| - @cli.add_input_param h - end - end - if @convert_options[:output] - @convert_options[:output].each do |h| - @cli.add_output_param h - end - end - end + def skip_transcode + log "Unsupported file #{path}" + dst << file.read + dst.close + end - begin - @cli.run - log "Successfully transcoded #{@basename} to #{dst}" - rescue Cocaine::ExitStatusError => e - raise Paperclip::Error, "error while transcoding #{@basename}: #{e}" if @whiny - end - else - log "Unsupported file #{@file.path}" - # If the file is not supported, just return it - dst << @file.read - dst.close + def run + begin + cli.run + log "Successfully transcoded #{basename} to #{dst}" + rescue Cocaine::ExitStatusError => e + raise Paperclip::Error, "error while transcoding #{basename}: #{e}" if whiny end - dst end - def log message - Paperclip.log "[transcoder] #{message}" + def configure_image + self.time = time.call(meta, options) if time.respond_to?(:call) + cli.filter_seek(time) + cli.filter_rotate(meta[:rotate]) if auto_rotate && !meta[:rotate].nil? end - def set_convert_options options - return options[:convert_options] if options[:convert_options].present? - options[:convert_options] = {output: {}} - return options[:convert_options] + def transfer_convert_options + convert_options.slice(:input, :output).each do |key, params| + params.each do |param| + param[1] = param[1].call(meta, options) if param[1].respond_to?(:call) + cli.send(:"add_#{key}_param", param) + end + end end - def format_geometry geometry - return unless geometry.present? - return geometry.gsub(/[#!<>)]/, '') + def log message + Paperclip.log "[transcoder] #{message}" end def output_is_image? - !!@format.to_s.match(/jpe?g|png|gif$/) + !!format.to_s.match(/jpe?g|png|gif$/) end end @@ -112,4 +89,5 @@ def meta instance_read(:meta) end end -end + +end \ No newline at end of file From 7b954d903961c8c70ea10d29fe293b3c8d39c91c Mon Sep 17 00:00:00 2001 From: Marc Tauber Date: Sat, 19 Jan 2019 15:13:17 +0000 Subject: [PATCH 2/2] No longer assuming that convert options are hashes --- lib/paperclip/paperclip_processors/transcoder.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/paperclip/paperclip_processors/transcoder.rb b/lib/paperclip/paperclip_processors/transcoder.rb index afc407a..7d979cd 100755 --- a/lib/paperclip/paperclip_processors/transcoder.rb +++ b/lib/paperclip/paperclip_processors/transcoder.rb @@ -67,6 +67,7 @@ def configure_image end def transfer_convert_options + return unless convert_options.is_a?(Hash) convert_options.slice(:input, :output).each do |key, params| params.each do |param| param[1] = param[1].call(meta, options) if param[1].respond_to?(:call)