Class: Paperclip::Transcoder
- Inherits:
-
Processor
- Object
- Processor
- Paperclip::Transcoder
- Defined in:
- lib/paperclip/paperclip_processors/transcoder.rb
Instance Attribute Summary collapse
-
#convert_options ⇒ Object
Returns the value of attribute convert_options.
-
#format ⇒ Object
Returns the value of attribute format.
-
#geometry ⇒ Object
Returns the value of attribute geometry.
-
#whiny ⇒ Object
Returns the value of attribute whiny.
Instance Method Summary collapse
- #format_geometry(geometry) ⇒ Object
-
#initialize(file, options = {}, attachment = nil) ⇒ Transcoder
constructor
Creates a Video object set to work on the
file
given. - #log(message) ⇒ Object
-
#make ⇒ Object
Performs the transcoding of the
file
into a thumbnail/video. - #output_is_image? ⇒ Boolean
- #set_convert_options(options) ⇒ Object
Constructor Details
#initialize(file, options = {}, attachment = nil) ⇒ Transcoder
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.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/paperclip/paperclip_processors/transcoder.rb', line 10 def initialize file, = {}, = 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 = [:whiny].nil? ? true : [:whiny] @convert_options = () @format = [:format] @geometry = [: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 = [:time].nil? ? 3 : [:time] @auto_rotate = [:auto_rotate].nil? ? false : [:auto_rotate] @pad_color = [:pad_color].nil? ? "black" : [:pad_color] @convert_options[:output][:s] = format_geometry(@geometry) if @geometry.present? .instance_write(:meta, @meta) if end |
Instance Attribute Details
#convert_options ⇒ Object
Returns the value of attribute convert_options.
3 4 5 |
# File 'lib/paperclip/paperclip_processors/transcoder.rb', line 3 def @convert_options end |
#format ⇒ Object
Returns the value of attribute format.
3 4 5 |
# File 'lib/paperclip/paperclip_processors/transcoder.rb', line 3 def format @format end |
#geometry ⇒ Object
Returns the value of attribute geometry.
3 4 5 |
# File 'lib/paperclip/paperclip_processors/transcoder.rb', line 3 def geometry @geometry end |
#whiny ⇒ Object
Returns the value of attribute whiny.
3 4 5 |
# File 'lib/paperclip/paperclip_processors/transcoder.rb', line 3 def whiny @whiny end |
Instance Method Details
#format_geometry(geometry) ⇒ Object
99 100 101 102 |
# File 'lib/paperclip/paperclip_processors/transcoder.rb', line 99 def format_geometry geometry return unless geometry.present? return geometry.gsub(/[#!<>)]/, '') end |
#log(message) ⇒ Object
89 90 91 |
# File 'lib/paperclip/paperclip_processors/transcoder.rb', line 89 def log Paperclip.log "[transcoder] #{}" end |
#make ⇒ Object
Performs the transcoding of the file
into a thumbnail/video. Returns the Tempfile that contains the new image/video.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/paperclip/paperclip_processors/transcoder.rb', line 44 def make ::Av.logger = Paperclip.logger @cli.add_source @file dst = Tempfile.new([@basename, @format ? ".#{@format}" : '']) dst.binmode if @meta log "Transocding 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 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 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 end dst end |
#output_is_image? ⇒ Boolean
104 105 106 |
# File 'lib/paperclip/paperclip_processors/transcoder.rb', line 104 def output_is_image? !!@format.to_s.match(/jpe?g|png|gif$/) end |
#set_convert_options(options) ⇒ Object
93 94 95 96 97 |
# File 'lib/paperclip/paperclip_processors/transcoder.rb', line 93 def return [:convert_options] if [:convert_options].present? [:convert_options] = {output: {}} return [:convert_options] end |