Skip to content

Instantly share code, notes, and snippets.

@gogojimmy
Forked from acnalesso/asset.rb
Created June 20, 2013 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gogojimmy/5819751 to your computer and use it in GitHub Desktop.
Save gogojimmy/5819751 to your computer and use it in GitHub Desktop.
# app/model/
# Your model where you've defined has_attached_file
# Do not define :original in your styles
class Asset < ActiveRecord::Base
attr_accessible :position, :picture_cover, :asset
belongs_to(:assetable, :polymorphic => true)
has_attached_file :asset,
:styles => {
:current => '1920x1080^',
:slide => '@1920x400#',
:gallery => '800x600',
:thumb => '400x370!',
:mini => '55x55'
},
:convert_options => {
:mini => ' -quality 75 -strip'
},
:hash_secret => "your-hash",
:default_url => "/assets/missing.gif",
:path => ":rails_root/public/system/:attachment/:id/:style/:digestive.:ext",
:url => "/system/:attachment/:id/:style/:digestive.:ext",
:processors => lambda { |i|
i.video? ? [:video_thumbnail] : [:thumbnail]
}
def asset_get(type)
asset.url(type)
end
def temp_thumbnail
false
end
Paperclip.interpolates(:digestive) do |attachment, style|
attachment.instance.digested
end
def digested
"#{self.id}-#{digest_asset_name}"
end
Paperclip.interpolates(:ext) do |attachment, s_name|
case
when ((style = attachment.styles[s_name]) && !style[:format].blank?) then style[:format]
when attachment.instance.video? && s_name.to_s != 'original' then 'jpg'
else
File.extname(attachment.original_filename).gsub(/^\.+/, "")
end
end
def video?
[ 'application/x-mp4',
'video/mpeg',
'video/quicktime',
'video/x-la-asf',
'video/x-ms-asf',
'video/x-msvideo',
'video/x-sgi-movie',
'video/x-flv',
'flv-application/octet-stream',
'video/3gpp',
'video/3gpp2',
'video/3gpp-tt',
'video/BMPEG',
'video/BT656',
'video/CelB',
'video/DV',
'video/H261',
'video/H263',
'video/H263-1998',
'video/H263-2000',
'video/H264',
'video/JPEG',
'video/MJ2',
'video/MP1S',
'video/MP2P',
'video/MP2T',
'video/mp4',
'video/MP4V-ES',
'video/MPV',
'video/mpeg4',
'video/mpeg4-generic',
'video/nv',
'video/parityfec',
'video/pointer',
'video/raw',
'video/rtx' ].include?(self.asset.content_type)
end
private
require 'digest/md5'
def generate_name(name)
Digest::MD5.hexdigest(name)
end
def digest_asset_name
generate_name(self.asset_file_name)
end
end
# lib/paperclip_processors/
module Paperclip
class VideoThumbnail < Processor
attr_accessor :time_offset, :geometry, :whiny
def initialize(file, options = {}, attachment = nil)
super
@current_file = file
set_time_offset
set_geometries
set_whiny
set_basename
end
def make
create_tmp_file
exeception_rescuer { paperclip_runner(cmds) }
tmp_file
end
private
attr_reader :basename, :tmp_file, :current_file
def cmds
cmd = %Q[-itsoffset #{time_offset} -i "#{file_expander(current_file)}" -y -vcodec mjpeg -vframes 1 -an -f rawvideo ]
cmd << "-s #{geometry.to_s} " unless geometry.nil?
cmd << %Q["#{file_expander(tmp_file)}"]
end
def file_expander(obj)
File.expand_path(obj.path)
end
def paperclip_runner(cmd)
Paperclip.run('avconv', cmd)
end
def create_tmp_file
@tmp_file = Tempfile.new([ basename, 'jpg' ].compact.join("."))
tmp_file.binmode
end
def set_time_offset
@time_offset = options[:time_offset] || '-4'
end
def set_whiny
@whiny = options[:whiny].nil? ? true : options[:whiny]
end
def set_basename
@basename = File.basename(file.path, File.extname(file.path))
end
def set_geometries
@geometry = Geometry.parse(options[:geometry])
assign_geometries unless geometry_options_nil?
end
def assign_geometries
geometry.width = (@geometry.width / 2.0).floor * 2.0
geometry.height = (@geometry.height / 2.0).floor * 2.0
geometry.modifier = ''
end
def geometry_options_nil?
options[:geometry].nil?
end
def exeception_rescuer
begin
yield
rescue Cocaine::CommandNotFoundError
exeception_raiser if whiny
end
end
def exeception_raiser
raise PaperclipError, "There was an error processing the thumbnail for #{basename}"
end
end
end
@acnalesso
Copy link

Please checkout the last update.
I've added a new method to close the opened files.

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment