Paperclip-ffmpeg error with styles for thumbnail when uploading video - ruby-on-rails

I am using Paperclip 3.4 and Paperclip-ffmpeg 0.9.4.
Here is my code in video.rb:
has_attached_file :file,
:url => '/:class/:id/:style.:extension',
:styles => { :thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10 } },
:processors => [:ffmpeg], :swallow_stderr => false
When I try to upload a video, the error is
Command 'ffmpeg -ss 10 -i :source -y -vf scale=100:-1,pad=100:100:0:12.5:black -vframes 1 -f image2 :dest' returned 1. Expected 0
If I comment out or remove the styles line, the video is uploaded correctly. I tried changing the versions of the gems, to no avail.
Any pointers on why the styles hash is causing an issue?
Edit
Replacing '#' with '>' seems to solve the issue, but I am not sure what is the cause.

In the official paper clip gem documentation (under the "Resizing options" section) you can found the following description:
Default behavior is to resize the image and maintain aspect ratio (i.e. the :medium version of a 300×150 image will be 200×100). Some commonly used options are:
trailing #, thumbnail will be centrally cropped, ensuring the requested dimensions.
trailing >, thumbnail will only be modified if it is currently larger requested dimensions. (i.e. the :small thumb for a 120×80 original image will be unchanged)
and as it is pointed there you can check for more options here.

Related

Rails reduce image size on some %

i need any solution for reducing image size
I am using paperclip
has_attached_file :image, :styles => { :small =>{ :geometry =>"200 x 123>"}}
For example i have original image 1 Mb and after some magic i want image 300 Kb
and then upload it on aws
You can use the convert_options
has_attached_file :image,
:convert_options => { :thumb => '-quality 50' }
or you can try this one
has_attached_file :image => { :quality => :better }
Refer: Paperclip doc
Take a look at this
https://github.com/janfoeh/paperclip-optimizer
It's a gem made for image optimization.
If you need a better optimization, use Google PageSpeed module for apache or Nginx !

rails paperclip - changing aspect ratio of uploaded image

Using paper clip, how can I change the aspect ratio of uploaded image.
Which is easier? Doing with jcrop or paperclip ?
I think paperclip would be nice but not sure where/how to keep the config options.
As for the paperclip you can easy do this by specifying convert options directly in your model. For example:
has_attached_file :photo,
:preserve_files => true,
:styles => { :medium => "800x800>",
:small => "300x300>",
:thumb => "150x150>" },
:convert_options => { :medium => "-quality 70 -interlace Plane -strip",
:small => "-quality 70 -interlace Plane -strip",
:thumb => "-quality 70 -interlace Plane -strip" },
:default_url => "/images/missing.png"
you can use that way any ImageMagick's conver option.
All supported options are described here.
BTW: if you want it to be processed in background than add this to your Gemfile:
gem 'delayed_job'
gem 'delayed_job_active_record'
gem 'delayed_paperclip'
gem 'daemons'
and this to the model:
process_in_background :photo, queue: 'paperclip_processing'
to run/stop a daemon:
RAILS_ENV=production bin/delayed_job -n 2 start
RAILS_ENV=production bin/delayed_job stop
and to see the progress and manage the queue this is great:
gem 'delayed_job_web'
Enjoy.
You'll need to use ImageMagick to get Paperclip to crop images. Paperclip only handles the upload process - it doesn't crop or store the images for you
I would recommend looking at how to use Paperclip with ImageMagick, and then you'll have to find a way to populate your models' styles option with ImageMagick commands:
has_attached_file :image, styles: { medium: "[[imagemagick code]]" }
ImageMagick change aspect ratio without scaling the image

Rails Paperclip image compression compared to what Page Speed produces

I've set up paperclip in rails and everything is working hunky-dory (i actually had to google that...:).
I've noticed however that Page Speed tells me I could losslessly compress my thumbnail and large images (the ones that paperclip produces) further. Is there an option I can put into my model which does this? I've noticed that mod_deflate doesn't compress images (I'm using Firefox).
You can add compression to paperclip processing using the paperclip-compression gem.
In your Gemfile:
gem "paperclip-compression", "~> 0.1.1"
(of course run bundle install)
In your model:
has_attached_file :avatar,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:processors => [:thumbnail, :compression]
"jpegtran works by rearranging the compressed data (DCT coefficients), without ever fully decoding the image. Therefore, its transformations are lossless"
Note: if you are running on heroku, you'll need jpegtran, and optipng binaries added to your application. Here's a good article on running binaries on heroku.
You should do your own testing on various JPEG compression levels but I've noticed that I can bump ImageMagicks quality setting down to 75 and still not see any noticeable difference - with about a 30-40% file size savings.
My model looks like:
has_attached_file :photo,
:styles => {
:"185x138" => {
:geometry => "185x138>"
} },
:convert_options => {
:all => "-auto-orient",
:"185x138" => "-quality 75",
-quality 75 is for ImageMagick. If you're using a different processor you will need to adjust accordingly.
What about FFMPEG or AVCONV?
sudo apt-get install ffmpeg/avconv
= initializer
Paperclip.options[:command_path] = "/usr/bin/" # see `which ffmpeg`
= Modal
after_save :compress_with_ffmpeg
def compress_with_ffmpeg
[:thumb, :original, :medium].each do |type|
img_path = self.avtar.path(type)
Paperclip.run("ffmpeg", " -i #{img_path} #{img_path}")
end
end

Rails Paperclip content_type validation error after convert & display wrong error

I have a problem with the validation of the content type by the Paperclip plug-in:
image.rb (relevant extract)
has_attached_file :photo,
:styles => {:xlarge => "640x512>", :large => "350x280>", :medium => "180x144^",
:thumb => "100x80^", :original => "1280x1280>" },
:convert_options => {:xlarge => "-strip", :large => "-strip",
:medium => "-strip -gravity center -extent 180x144 +repage",
:thumb => "-strip -gravity center -extent 100x80 +repage"},
:default_style => :medium,
:url => "/system/:attachment/:id/:basename_:id_:style.:extension"
validates_attachment_presence :photo
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png', 'image/gif', 'image/pjpeg', 'image/x-png', 'image/jpeg2000'], :message => 'Uploaded file is not an image'
I'm using nested forms, the image model is the child model of the property model.
During testing the validations of my form I come across some problems. I'm testing validations by uploading a PDF file where the model accepts only images.
output in my form view of: <%= #property.errors.inspect %>
#[#, #message="Uploaded file is not an image">],
"images.photo"=>[#, #message="C:/Users/Michael/AppData/Local/Temp/stream,4272,1.pdf is not recognized by the 'identify' command.">]}>, #base=#>
output in the view of the validation error(s) on the file upload input box:
C:/Users/Michael/AppData/Local/Temp/stream,4272,1.pdf is not recognized by the 'identify' command.
C:/Users/Michael/AppData/Local/Temp/stream,4272,1.pdf is not recognized by the 'identify' command.
C:/Users/Michael/AppData/Local/Temp/stream,4272,1.pdf is not recognized by the 'identify' command.
C:/Users/Michael/AppData/Local/Temp/stream,4272,1.pdf is not recognized by the 'identify' command.
C:/Users/Michael/AppData/Local/Temp/stream,4272,1.pdf is not recognized by the 'identify' command.
2 issues:
issue 1
The input field shows 5 times the same error, that is because I have 5 styles set up in the model. Question is, why does paperclip try to identify (and probably convert the pdf), when you would expect the validation to run first and return already an error before trying to identify and convert? Does Paperclip convert to all the styles before running validations? If yes, is there a way to switch the order of processing, first validation, then styles processing?
issue 2
How can I display the error message of the validation ('Uploaded file is not an image') next to the file upload input box instead of the erros now displayed (5 x the output of the identify command), the latter is of no use to the website user.
Thanks!
I have used
has_attached_file :photo, :whiny => false
and this seems to have helped the error messages. For the error messages, I was using
OBJECT.errors[:photo_content_type]
OBJECT.errors[:photo_file_size]

How to show video clip in a list with Ruby on Rails.

I have uploaded videos but when I see them in list, I would like to see a snapshot or clip of the video. Is there a way to display a list videos like this?
You can use ffmpeg to generate images from a video.
You'll find here a tutorial allowing you to use Paperclip to upload a video and FFMpeg to make a thumbnail out of it.
In that tutorial, what's interesting for making the thumbnail is the make method.
Which generates a ffmpeg command and executes it.
You'll find informations about that command alone here.
When you upload videos & images with Paperclip, it uses a processor to create different variations of the file
The processor depends on the type of file, and will typically be imagemagick if you're working with images, and ffmpeg if you're working with video
The way this works is that Paperclip will handle the file upload, and then send to the various processor to change. That's how you can generate the thumbnails with imagemagick, or the video thumbnail with ffmpeg
paperclip-ffmpeg
FFMpeg will be the best way to create a video thumbnail, and you can actually put it to work by installing the paperclip-ffmpeg gem
Here is some live code showing how this works (you basically just have to call the :ffmpeg processor:
has_attached_file :attachment,
styles: lambda { |a| a.instance.is_image? ? {:small => "x200>", :medium => "x300>", :large => "x400>"} : {:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10}, :medium => { :geometry => "300x300#", :format => 'jpg', :time => 10}}},
:processors => lambda { |a| a.is_video? ? [ :ffmpeg ] : [ :thumbnail ] }
def is_video?
attachment.instance.attachment_content_type =~ %r(video)
end
def is_image?
attachment.instance.attachment_content_type =~ %r(image)
end
If you need some more help with this (obviously the question was a long time ago), please let me know!

Resources