diff --git a/lib/paperclip/paperclip_processors/transcoder.rb b/lib/paperclip/paperclip_processors/transcoder.rb index d191ce6..27d492f 100755 --- a/lib/paperclip/paperclip_processors/transcoder.rb +++ b/lib/paperclip/paperclip_processors/transcoder.rb @@ -8,10 +8,14 @@ class Transcoder < Processor # +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 + log "Options: #{options.to_s}" + log "Attachment: #{attachment.inspect}" + @file = file @current_format = File.extname(@file.path) @basename = File.basename(@file.path, @current_format) @cli = ::Av.cli + @style = options[:style] || 'default' @meta = ::Av.cli.identify(@file.path) @whiny = options[:whiny].nil? ? true : options[:whiny] @@ -30,13 +34,17 @@ def initialize file, options = {}, attachment = nil @shrink_only = @keep_aspect && modifier == '>' end + @exif_data = MiniExiftool.new(@file.path) + @meta[:rotate] = @exif_data.rotation + log "Rotation data: #{@meta[:rotate]}" + @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] @convert_options[:output][:s] = format_geometry(@geometry) if @geometry.present? - attachment.instance_write(:meta, @meta) if attachment + @attachment = attachment end # Performs the transcoding of the +file+ into a thumbnail/video. Returns the Tempfile @@ -56,20 +64,25 @@ def make if output_is_image? @time = @time.call(@meta, @options) if @time.respond_to?(:call) @cli.filter_seek @time - - if @auto_rotate && !@meta[:rotate].nil? - log "Adding rotation #{@meta[:rotate]}" - case @meta[:rotate] - when 90 - @convert_options[:output][:vf] = "'transpose=1'" - when 180 - @convert_options[:output][:vf] = "'vflip, hflip'" - when 270 - @convert_options[:output][:vf] = "'transpose=2'" - end - end end + # if @auto_rotate && !@meta[:rotate].nil? + # log "Adding rotation #{@meta[:rotate]}" + # arg = case @meta[:rotate] + # when 90 then 'transpose=1' + # when 180 then 'vflip, hflip' + # when 270 then 'transpose=2' + # end + # if arg + # if @convert_options[:output][:vf] + # @convert_options[:output][:vf] += ", #{arg}" + # else + # @convert_options[:output][:vf] = "#{arg}" + # end + # end + # @convert_options[:output][:vf] = "'#{@convert_options[:output][:vf]}'" + # end + if @convert_options.present? if @convert_options[:input] @convert_options[:input].each do |h| @@ -86,6 +99,19 @@ def make begin @cli.run log "Successfully transcoded #{@basename} to #{dst}" + + exif_data = MiniExiftool.new(dst.path) + log "Exif data: #{exif_data.inspect}" + @meta[:output] ||= {} + # set metadata correctly if image is rotated + if exif_data.rotatation == 90 || exif_data.rotation == 270 + @meta[:output][:width] = exif_data.imageheight + @meta[:output][:height] = exif_data.imagewidth + else + @meta[:output][:width] = exif_data.imagewidth + @meta[:output][:height] = exif_data.imageheight + end + rescue Cocaine::ExitStatusError => e raise Paperclip::Error, "error while transcoding #{@basename}: #{e}" if @whiny end @@ -95,6 +121,17 @@ def make dst << @file.read dst.close end + + # todo: maybe clean this up? attachment model can handle the read/write + # of metadata. + if @attachment + @attachment_meta = @attachment.meta || {} + @attachment_meta[@style.to_sym] = @meta + json_meta = JSON.dump(@attachment_meta) + log "JSON metadata: #{json_meta}" + @attachment.instance_write(:meta, json_meta) + end + dst end @@ -120,7 +157,8 @@ def output_is_image? class Attachment def meta - instance_read(:meta) + data = instance_read(:meta) + data ? JSON.load(instance_read(:meta)) : nil end end end diff --git a/paperclip-av-transcoder.gemspec b/paperclip-av-transcoder.gemspec index c62aa5c..b1500ed 100644 --- a/paperclip-av-transcoder.gemspec +++ b/paperclip-av-transcoder.gemspec @@ -27,4 +27,6 @@ Gem::Specification.new do |spec| spec.add_dependency "paperclip", ">=2.5.2" spec.add_dependency "av", "~> 0.9.0" + spec.add_dependency "json", "~> 1.8.3" + spec.add_dependency "mini_exiftool", ">=2.5.1" end