Async video processing in rails and paperclip - ruby-on-rails

I'm using gem 'paperclip-av-transcoder', "0.6.2" for my video processing.
I simply need to write this code to have my video converted into many formats:
has_attached_file :video, :styles => {
:mp4 => { :format => 'mp4', :convert_options => { :output => { :vcodec => 'libx264', :acodec => 'copy' } } },
:ogg => { :format => 'ogg', :auto_rotate => true },
:webm => { :format => 'webm', :auto_rotate => true },
:flv => { :format => 'flv', :convert_options => { :output => { :ar => 44100 } }, :auto_rotate => true },
:thumb => { :geometry => "300x300#", :format => 'jpg', :time => 1, :auto_rotate => true }
}, :processors => [:transcoder]
But this code is synchronous and the user that upload the video is stuck during the conversion.
I would like to add an async wrapper (worker) around :transcoder so I can call:
has_attached_file :video, :styles => {... }, :processors => [:wrapper_transcoder_worker]
I have installed resque and redis and everything work.
Do you have an idea on how can I write the asynch worker around transcoder?

Have you tried delayed_paperclip gem? It pushes all processing into background and supports resque (among others).

lock paperclip at version 4.2.0

Related

i can not upload video in background with delayed_job on heroku?

I used paperclip background process with delay jobs with ffmpeg.
In paperclip processors folder i have ffmpeg.rb file also.
my model
class product < ActiveRecord::Base
has_attached_file :video, :styles => {
:mp4video => { :geometry => "640x480", :format => 'mp4', :convert_options => {:output => {:ar => 44100}} },
:webmvideo =>{ :geometry => "1024x576", :format => 'webm', :convert_options => {:output => {:ar => 44100}} },
:oggvideo => { :geometry => "1024x576", :format => 'ogg', :convert_options => {:output => {:ar => 44100}} },
:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10 }
},
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/aws.yml",
:processors => [:transcoder]
process_in_background :video
end

Get action name in model rails 4

I am using paperclip to upload files in my rail4 project. Now I need to apply some condition based on the name of controller action that is currently uploading the attachment file.
Can someone please help me get the name of currently used action in model.
Thanks in advance.
UPDATE:
Here is my code in modal file:
has_attached_file :attachment, :styles => lambda { |attachment| {:medium => attachment.instance.video? ? { :geometry => "236x236>", :format => 'jpg', :time => 10 } : { :geometry => "236x236>" }, :thumb => attachment.instance.video? ? { :geometry => "150x150>", :format => 'jpg', :time => 10 } : { :geometry => "150x150>" },:large => attachment.instance.video? ? { :geometry => "1000x700", :format => 'jpg', :time => 10 } : { :geometry => "1000x700" }} },:processors => lambda { |a| a.video? ? [ :transcoder ] : [ :cropper ] }
before_post_process :is_audio?
def is_audio?
return false if [ 'audio/mp3','audio/mpeg'].include?(attachment.content_type)
end
Now I want to skip processors and styles and keep only the original file (just like here in case of audio file) if file is uploaded from update action. Please suggest.

Error updating styles paperclip

I want to update my styles in paperclip, but when i run the command, a error is returned. Here is the code of my styles:
image.rb
class Image < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
validates_presence_of :image
has_attached_file :image, :processors => [:ffmpeg],
:styles => { :video => { :geometry => "640x480", :format => 'mp4' },
:thumb => { :geometry => "100x100>", :format => 'jpg' },
:large => { :geometry => "400x300>", :format => 'jpg' },
:medium => { :geometry => "400x200>", :format => 'png' },
:icon => { :geometry => "36x36>", :format => 'jpg' },
:profile => { :geometry => "28x28>", :format => 'jpg' } },
:default_url => "/images/:style/missing.png"
end
Returned Error:
Regenerating Image -> image -> [:icon, :large, :medium, :profile, :thumb, :video]
rake aborted!
Cocaine::CommandNotFoundError
How i solve this error?
It seems to be that you have not installed the "paperclip-ffmpeg" gem.
See this link: https://github.com/owahab/paperclip-ffmpeg#requirements

ActionView::Template::Error (can't dup Symbol): -- Issue with AWS only in case of quality decrease for image

I was doing some image optimization on my site. Things are working awesome on development machine but in production mode I am getting an error ie:
ActionView::Template::Error (can't dup Symbol):
This is happening only in case of aws, If I remove this then things working perfectly fine:
A code I was using and was working perfectly:
has_attached_file :attachment, {
:styles => {
:medium=>"654x346>",
:small => "260x400>",
:thumb => "75x75#",
:facebook_meta_tag => "200x200#"
}, :include_updated_timestamp => false
}.merge(PAPERCLIP_STORAGE_OPTIONS)
But now I modified this and Its working perfectly fine in development environment but not working on production environment.
has_attached_file :attachment,
:styles => {
:medium => {
:geometry => "654x346>",
:quality => 60,
:format => 'JPG'
},
:small => {
:geometry => "260x400>",
:quality => 60,
:format => 'JPG'
},
:thumb => {
:geometry =>"75x75#",
:quality => 60,
:format => 'JPG'
},
:facebook_meta_tag => {
:geometry =>"200x200#",
:quality => 50,
:format => 'JPG'
}
}.merge(PAPERCLIP_STORAGE_OPTIONS)
Other relevant settings:
PAPERCLIP_STORAGE_OPTIONS = {
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/amazons3.yml",
}
amazons3.yml
production:
access_key_id: XXXXxxxXXXXxxx
secret_access_key: XXXXxxxXXXXxxxXXXXxxxXXXXxxxXXXXxxxXXXXxxx
bucket: images.XXXXxxx.com
Here is the working code:
has_attached_file :attachment, {
:styles => {
:medium => ["654x346>", :jpg],
:small => ["260x400>", :jpg],
:thumb => ["75x75#", :jpg],
:facebook_meta_tag =>["200x200#", :jpg]
},
:convert_options => {
:medium => "-quality 60",
:small => "-quality 60",
:thumb => "-quality 60",
:facebook_meta_tag => "-quality 60"
}
}.merge(PAPERCLIP_STORAGE_OPTIONS)
ActionView::Template::Error tends to be an error raised inside of a view (Rails re-wraps errors inside views inside ActionView::Template::Error exceptions).
To help you more I'd need to see the backtrace and the view involved.
This is not really an answer, but if this helps you find the cause you may as well accept it.

Paperclip dynamic Proc styles called before object initialized

I have the following paperclip setup. What happens is that I'm using a proc to set the sizes for various styles. However, the proc gets called on new and during the super call. I walked through the debugger and it seems like it processes the :photo parameter first so it initializes the attachment and calls the styles proc at which point the actual object (Photo) has not been initialized by the passed in params(particularly the photo.gallery_id so it doesn't set the styles correctly. I even tried reprocessing and it didn't help. I've spent a couple of days on this and still no luck. any help is appreciated!
class Photo < ActiveRecord::Base
has_and_belongs_to_many :staffs
has_attached_file :photo,
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/assets/:id/:class/:style/:image_name.:extension",
:url => "/assets/:id/:class/:style/:image_name.:extension",
:styles => Proc.new { |clip| clip.instance.attachment_styles}
def attachment_styles
if self.gallery.nil?
{ :original => {
:processors => [:watermark],
:geometry =>"600x800!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'},
:thumbnail => {
:processors => [:watermark],
:geometry => "200x300!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'}
}
elsif self.photo.styles.empty?
gallery_type = GalleryType.find_by_id(self.gallery_id)
{ :original => {
:processors => [:watermark],
:geometry =>"#{gallery_type.width_max}x#{gallery_type.height_max}!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'},
:thumbnail => {
:processors => [:watermark],
:geometry => "#{gallery_type.width_min}x#{gallery_type.height_min}!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'}
}
else
self.photo.styles
end
end
def reprocess_att
self.photo.reprocess!
end
def initialize(galleryid, params = {})
begin
param.merge!({"gallery_id" => galleryid.to_s})
super(params)
rescue => e
puts e.message()
end
end
From what I can see, the order of the parameters is important. I had:
attachments.build(:upload => File.new(File.dirname(__FILE__) + '/../fixtures/test-image.jpg'),
:styles => {:small => ['100x100#', :jpg], :medium => ['250x250', :jpg]})
And this wasn't setting up the styles correctly. They they were nil. I changed it to:
attachments.build(:styles => {:small => ['100x100#', :jpg], :medium => ['250x250', :jpg]},
:upload => File.new(File.dirname(__FILE__) + '/../fixtures/test-image.jpg'))
And then the code:
:styles => lambda { |a| a.instance.styles || {} }
worked perfectly. Hope this helps.
Thanks for the answer!
I've been fighting with this for a few weeks now. I'm using Paperclip with FFMPEG to make thumbnails of uploaded videos. I have an option to set what frame to use as the thumbnail.
I'm also using a nested form (awesome nested forms) for my asset upload. So what I did was put the frame time param before the file browse button. This solved the problem for me since I'm not using builder.

Resources