lib/paperclip_processors/ffmpeg.rb
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 = []

      if @auto_rotate
        Ffmpeg.log("Adding rotation #{@meta[:rotate]}") if @whiny
        # If file has rotation, rotate it to horizontal.
        if @auto_rotate == "landscape"
          if @meta[:rotate] == 90
            @convert_options[:output][:vf] = 'transpose=1'
            rotated = true
          end
        end

        # If file has rotation, rotate it to portrait.
        if @auto_rotate == "portrait"
          if @meta[:rotate] == 0
            @convert_options[:output][:vf] = 'transpose=1'
            rotated = true
          end
        end
      end

      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 @meta[:size].nil?
          Ffmpeg.log("Target Size is Available") if @whiny
          current_width, current_height = @meta[: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 / (@meta[:aspect].to_f)).to_i
                @convert_options[: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, height = calculate_output_file_dimensions(target_width, target_height)
                @convert_options[: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, height = calculate_output_file_dimensions(target_width, target_height)
              # 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
              @convert_options[:output][:vf][/\z/] = ',' if @convert_options[:output][:vf]
              @convert_options[:output][:vf] ||= ''
              if pad_y > 0 && rotated.nil?
                @convert_options[:output][:vf][/\z/] = "scale=#{width}:-1,pad=#{width.to_i}:#{target_height.to_i}:0:#{pad_y}:#@pad_color"
              else
                @convert_options[:output][:vf][/\z/] = "scale=#{width}:-1,crop=#{width.to_i}:#{width.to_i}"
              end
              Ffmpeg.log("Convert Options: #{@convert_options[:output][:s]}") if @whiny
            else
              Ffmpeg.log("Resize") if @whiny
              # Keep aspect ratio
              width, height = calculate_output_file_dimensions(target_width, target_height)
              @convert_options[: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
            @convert_options[: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

      Ffmpeg.log("Adding Format") if @whiny
      # Add format
      case @format
      when 'jpg', 'jpeg', 'png', 'gif' # Images
        @convert_options[:input][:ss] = @time
        @convert_options[:output][:vframes] = 1
        @convert_options[:output][:f] = 'image2'
      when 'webm' # WebM
        @convert_options[:output][:acodec] = 'libvorbis'
        @convert_options[:output][:vcodec] = 'libvpx'
        @convert_options[:output][:f] = 'webm'
      when 'ogv' # Ogg Theora
        @convert_options[:output][:acodec] = 'libvorbis'
        @convert_options[:output][:vcodec] = 'libtheora'
        @convert_options[:output][:f] = 'ogg'
      when 'mp4'
        @convert_options[:output][:acodec] = 'aac'
        @convert_options[:output][:strict] = 'experimental'
      end

      Ffmpeg.log("Adding Source") if @whiny
      # Add source
      # Validations on the values. These could be either nil.
      parameters << @convert_options[:input].map { |k,v| "-#{k.to_s} #{v} " if !v.nil? && (v.is_a?(Numeric) || !v.empty?) }
      parameters << "-i :source"
      parameters << @convert_options[: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.expand_path(dst.path))
      rescue Cocaine::ExitStatusError => e
        raise Paperclip::Error, "error while processing video for #{@basename}: #{e}" if @whiny
      end

      dst # Return tempfile
    end
# Performs the transcoding of the +file+ into a thumbnail/video. Returns the Tempfile
# that contains the new image/video.
Performs the transcoding of the +file+ into a thumbnail/video. Returns the Tempfile
that contains the new image/video.
Performs the transcoding of the + file + into a thumbnail / video . Returns the Tempfile that contains the new image / video .
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 = []
      if @auto_rotate
        Ffmpeg.log("Adding rotation #{@meta[:rotate]}") if @whiny
        if @auto_rotate == "landscape"
          if @meta[:rotate] == 90
            @convert_options[:output][:vf] = 'transpose=1'
            rotated = true
          end
        end
        if @auto_rotate == "portrait"
          if @meta[:rotate] == 0
            @convert_options[:output][:vf] = 'transpose=1'
            rotated = true
          end
        end
      end
      Ffmpeg.log("Adding Geometry") if @whiny
      if @geometry
        Ffmpeg.log("Extracting Target Dimensions") if @whiny
        if @geometry =~ /(\d*)x(\d*)/
          target_width = $1
          target_height = $2
        end
        unless @meta[:size].nil?
          Ffmpeg.log("Target Size is Available") if @whiny
          current_width, current_height = @meta[:size].split('x')
          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
                width = target_width.to_i
                height = (width.to_f / (@meta[:aspect].to_f)).to_i
                @convert_options[: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
              end
            elsif @shrink_only
              Ffmpeg.log("Shrink Only") if @whiny
              if current_width.to_i > target_width.to_i
                width, height = calculate_output_file_dimensions(target_width, target_height)
                @convert_options[: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
              end
            elsif @pad_only
              Ffmpeg.log("Pad Only") if @whiny
              width, height = calculate_output_file_dimensions(target_width, target_height)
              pad_y = (target_height.to_f - height.to_f) / 2
              @convert_options[:output][:vf][/\z/] = ',' if @convert_options[:output][:vf]
              @convert_options[:output][:vf] ||= ''
              if pad_y > 0 && rotated.nil?
                @convert_options[:output][:vf][/\z/] = "scale=#{width}:-1,pad=#{width.to_i}:#{target_height.to_i}:0:#{pad_y}:#@pad_color"
              else
                @convert_options[:output][:vf][/\z/] = "scale=#{width}:-1,crop=#{width.to_i}:#{width.to_i}"
              end
              Ffmpeg.log("Convert Options: #{@convert_options[:output][:s]}") if @whiny
            else
              Ffmpeg.log("Resize") if @whiny
              width, height = calculate_output_file_dimensions(target_width, target_height)
              @convert_options[: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
            @convert_options[: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
      Ffmpeg.log("Adding Format") if @whiny
      case @format
      when 'jpg', 'jpeg', 'png', 'gif' 
        @convert_options[:input][:ss] = @time
        @convert_options[:output][:vframes] = 1
        @convert_options[:output][:f] = 'image2'
      when 'webm' 
        @convert_options[:output][:acodec] = 'libvorbis'
        @convert_options[:output][:vcodec] = 'libvpx'
        @convert_options[:output][:f] = 'webm'
      when 'ogv' 
        @convert_options[:output][:acodec] = 'libvorbis'
        @convert_options[:output][:vcodec] = 'libtheora'
        @convert_options[:output][:f] = 'ogg'
      when 'mp4'
        @convert_options[:output][:acodec] = 'aac'
        @convert_options[:output][:strict] = 'experimental'
      end
      Ffmpeg.log("Adding Source") if @whiny
      parameters << @convert_options[:input].map { |k,v| "-#{k.to_s} #{v} " if !v.nil? && (v.is_a?(Numeric) || !v.empty?) }
      parameters << "-i :source"
      parameters << @convert_options[: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.expand_path(dst.path))
      rescue Cocaine::ExitStatusError => e
        raise Paperclip::Error, "error while processing video for #{@basename}: #{e}" if @whiny
      end
      dst 
    end
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 = [ ] if @auto_rotate Ffmpeg . log ( "Adding rotation #{@meta[:rotate]}" ) if @whiny if @auto_rotate == "landscape" if @meta [ :rotate ] == 90 @convert_options [ :output ] [ :vf ] = 'transpose=1' rotated = true end end if @auto_rotate == "portrait" if @meta [ :rotate ] == 0 @convert_options [ :output ] [ :vf ] = 'transpose=1' rotated = true end end end Ffmpeg . log ( "Adding Geometry" ) if @whiny if @geometry Ffmpeg . log ( "Extracting Target Dimensions" ) if @whiny if @geometry =~ / ( \d *)x( \d *) / target_width = $1 target_height = $2 end unless @meta [ :size ] . nil? Ffmpeg . log ( "Target Size is Available" ) if @whiny current_width , current_height = @meta [ :size ] . split ( 'x' ) 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 width = target_width . to_i height = ( width . to_f / ( @meta [ :aspect ] . to_f ) ) . to_i @convert_options [ :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 end elsif @shrink_only Ffmpeg . log ( "Shrink Only" ) if @whiny if current_width . to_i > target_width . to_i width , height = calculate_output_file_dimensions ( target_width , target_height ) @convert_options [ :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 end elsif @pad_only Ffmpeg . log ( "Pad Only" ) if @whiny width , height = calculate_output_file_dimensions ( target_width , target_height ) pad_y = ( target_height . to_f - height . to_f ) / 2 @convert_options [ :output ] [ :vf ] [ / \z / ] = ',' if @convert_options [ :output ] [ :vf ] @convert_options [ :output ] [ :vf ] ||= '' if pad_y > 0 && rotated . nil? @convert_options [ :output ] [ :vf ] [ / \z / ] = "scale=#{width}:-1,pad=#{width.to_i}:#{target_height.to_i}:0:#{pad_y}:#@pad_color" else @convert_options [ :output ] [ :vf ] [ / \z / ] = "scale=#{width}:-1,crop=#{width.to_i}:#{width.to_i}" end Ffmpeg . log ( "Convert Options: #{@convert_options[:output][:s]}" ) if @whiny else Ffmpeg . log ( "Resize" ) if @whiny width , height = calculate_output_file_dimensions ( target_width , target_height ) @convert_options [ :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 @convert_options [ :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 Ffmpeg . log ( "Adding Format" ) if @whiny case @format when 'jpg' , 'jpeg' , 'png' , 'gif' @convert_options [ :input ] [ :ss ] = @time @convert_options [ :output ] [ :vframes ] = 1 @convert_options [ :output ] [ :f ] = 'image2' when 'webm' @convert_options [ :output ] [ :acodec ] = 'libvorbis' @convert_options [ :output ] [ :vcodec ] = 'libvpx' @convert_options [ :output ] [ :f ] = 'webm' when 'ogv' @convert_options [ :output ] [ :acodec ] = 'libvorbis' @convert_options [ :output ] [ :vcodec ] = 'libtheora' @convert_options [ :output ] [ :f ] = 'ogg' when 'mp4' @convert_options [ :output ] [ :acodec ] = 'aac' @convert_options [ :output ] [ :strict ] = 'experimental' end Ffmpeg . log ( "Adding Source" ) if @whiny parameters << @convert_options [ :input ] . map { | k , v | "-#{k.to_s} #{v} " if ! v . nil? && ( v . is_a? ( Numeric ) || ! v . empty? ) } parameters << "-i :source" parameters << @convert_options [ :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 . expand_path ( dst . path ) ) rescue Cocaine :: ExitStatusError => e raise Paperclip :: Error , "error while processing video for #{@basename}: #{e}" if @whiny end dst end
Performs the transcoding of the +file+ into a thumbnail/video.
Performs the transcoding of the + file + into a thumbnail / video .
# If file has rotation, rotate it to horizontal. # If file has rotation, rotate it to portrait. # Add geometry # Extract target dimensions # Only calculate target dimensions if we have current dimensions # Current width and height # Keep aspect ratio #return nil # Keep aspect ratio #return nil # Keep aspect ratio # We should add half the delta as a padding offset Y # There could be options already set # Keep aspect ratio # Do not keep aspect ratio # Add format # Images # WebM # Ogg Theora # Add source # Validations on the values. These could be either nil. # Return tempfile