Ok I’ve gotten it to work. For everyone that is interested or devs who
stumble across:
Model class:
has_attached_file :media,
:styles => { :small => ‘94x58#’,
:medium => ‘188x116#’,
:large => ‘376x232#’ },
:url => ‘/assets/artists/:artist_id/
videos/:id/:style.:extension’,
:path => ‘:rails_root/public/assets/
artists/:artist_id/videos/:id/:style.:extension’,
:processors => [:video_thumbnail]
/lib/paperclip_processors/video_thumbnail.rb:
module Paperclip
class VideoThumbnail < Processor
attr_accessor :geometry, :whiny
def initialize(file, options = {}, attachment = nil)
super
@file = file
unless options[:geometry].nil? || (@geometry =
Geometry.parse(options[:geometry])).nil?
@geometry.width = (@geometry.width / 2.0).floor * 2.0
@geometry.height = (@geometry.height / 2.0).floor * 2.0
@geometry.modifier = ‘’
end
@whiny = options[:whiny].nil? ? true : options[:whiny]
@basename = File.basename(file.path, File.extname(file.path))
end
def make
dst = Tempfile.new([ @basename, 'jpg' ].compact.join("."))
dst.binmode
cmd = "ffmpeg -itsoffset -4 -i #{@file.path} -y -vcodec mjpeg -
vframes 1 -an -f rawvideo -s #{geometry.to_s}
#{File.expand_path(dst.path)}"
begin
success = Paperclip.run(cmd)
rescue PaperclipCommandLineError
raise PaperclipError, "There was an error processing the
thumbnail for #{@basename}" if whiny
end
dst
end
end
end
/config/initializers/paperclip.rb
Paperclip.interpolates :artist_id do |attachment, style|
attachment.instance.artist.id
end
Ensures that files have the proper extensions. i.e. thumbnails with
jpgs
Paperclip.interpolates :extension do |attachment, style_name|
case attachment.instance.class.to_s
when “Video”
if style_name.to_s == ‘original’
‘flv’
else
‘jpg’
end
when “Audio”
if style_name.to_s == ‘original’
‘mp3’
end
else
File.extname(attachment.original_filename).gsub(/^.+/, “”)
end
end
Path to ffmpeg
Paperclip.options[:command_path] = “/opt/local/bin”
Hope this helps somebody
On Oct 2, 4:44 pm, Christian F. [email protected]