Rails using ffmpeg to make a screenshot of a video - ruby-on-rails

I want to use ffmpeg to make a screenshot of a uploaded video.
What I do is: uploading a video with carrierwave to amazonS3
when or while it is uploading I want to make a screenshot as thumbnail for this video.
How can I make this? How can I call ffmpeg with rails?
Thanks for your help

To do that, we are going to use the gem streamio-ffmpeg to run our FFMPEG commands from a rails library
require 'streamio-ffmpeg'
module ControllerVideoProcessor
def thumbnail path, second
movie = FFMPEG::Movie.new(path)
return movie.screenshot("some/temporal/path/screenshot.jpg", :seek_time => second)
end
end
As we can see, we have a function, which receives the path to the input video, and the second we want to get the thumbnail from. It is as simple as running the “screenshot” command of the streamio library, and that’s it. It will return a FFMPEG object, containing the image and it’s attributes.
Also if you use carrierwave gem for uploading your files you can use carrierwave plugin gem 'video_thumbnailer'
example
class VideoUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
include VideoThumbnailer
storage :file
version :thumb do
process generate_thumb:[{quality:5, time_frame: '00:0:01', file_extension: 'jpeg'}]
def full_filename for_file
png_name for_file, version_name, "jpeg"
end
end
def png_name for_file, version_name, format
%Q{#{version_name}_#{for_file.chomp(File.extname(for_file))}.#{format}}
end
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w( mp4 jpg jpeg gif png )
end
end
Reference and more information about it you can find it here
http://ron-on-rails.tumblr.com/post/33720054493/getting-thumbnails-of-a-video-using-ffmpeg-and
https://github.com/teenacmathew/Video-Thumbnailer

You could use some gem that can talk to ffmpeg like this gem: https://github.com/streamio/streamio-ffmpeg
or you could call it through the command line similar to what is suggested in this question: Calling shell commands from Ruby

Related

Image uploading issue with Carrierwave and Minimagick (Ruby on rails)

I am using Carrierwave to upload images and that works fine but I am now trying to use Minimagick to process these uploaded images.
What is going on?
The images still upload as well as their thumb version, however both images are exactly the same size.
Here is the uploader code:
class FileUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# Override the directory where uploaded files will be stored.
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_fit => [100, 100]
end
# Add a white list of extensions which are allowed to be uploaded.
def extension_white_list
%w(jpg jpeg gif png)
end
end
And here is how I am calling it:
<%= image_tag example.my_file.url(:thumb).to_s %>
I dont think it is anything to do with the way I am calling it because the two files created by all this are the same, so sound slike its to do with processing it on upload.
Here is the folder created for each object:
Image
--- Fixed ---
The issue was ImageMagick. It was not properly configured on my machine so that means no gem that deals with image processing (Paperclip, Minimagick or Dragonfly) could complete any image manipulation.

Carrierwave undefined metho auto_orient

I've been trying to use RMagick auto_orient method to fix mobile uploads. Currently they are rotated 90 degrees. My uploader file currently looks like this.
class AvatarUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :fog
def root
Rails.root.join 'public/'
end
include CarrierWave::MimeTypes
process :set_content_type
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
process :resize_to_fill => [200, 200]
version :thumb do
process :resize_to_fill => [50, 50]
end
process :auto_orient
def extension_white_list
%w(jpg jpeg gif png)
end
end
This is giving me an error
undefined local variable or method auto_orient for AvatarUploader:Class (NameError)
I've tried several solutions,
exif image rotation issue using carrierwave and rmagick to upload to s3, https://github.com/minimagick/minimagick/issues/68
but no dice.
Anyone got a clue what I'm doing wrong?
Try adding the following:
def auto_orient
manipulate! do |img|
img.auto_orient!
end
end
As it stands now, the auto_orient process you're referencing doesn't exist in the context, hence the error.
Edit: according to the imagemagick github link you posted, auto_orient! might be broken. You could then use auto_orient instead in a similar way (it just creates a new image instead of modifying the one passed to the method). Refer to the links you posted for possible solutions using the auto_orient method.

Minimagick gem fails to convert multipage PDF to several png images

I am new to RoR and I'm trying to develop a slide sharing app through which users can upload and share a multipage PDFs containing a deck of slides (one slide per page). Viewers can then view these slides on a "show" page.
Behind the scenes I am trying to convert the multipage pdf into several .png files (1 pdf page -> 1.png file), and then display them in a carousel/slider fashion.
Here is my uploader:
# encoding: utf-8
class DocUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
# 'public/doc_uploads/'
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
process :filename
process :generate_png
def generate_png
pdf = MiniMagick::Image.new(self.file.path)
pdf.pages.each_with_index do |page, index|
page.write("page#{index}.png")
end
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(pdf)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
def filename
'doc.pdf' if original_filename
end
end
When I try to upload a multipage pdf document, it throws me this error:
'identify /public/uploads/tmp/1443619784-406-3861/doc.pdf' failed with error: at the line pdf.pages.each_with_index do |page, index|
I have tried different solutions to converting the document, but none of them have worked for me so far. Why would a run of the mill .pdf file fail minimagick's validation? Is there anything I can do to avoid running into this issue? Any advice will be greatly appreciated!

cant see the aws-s3 url saved in rails database

I have successfully implemented image uploading using carrierwave, fog and Amazon S3. In my imageuploader am using only fog as storage. But when i check my database i can see that just the file name is written instead of the amazon url. In my views its fetching correctly from aws without any issues.
Is it supposed to be like this?
If so how the application figure out the exact url to s3?
imageuploader.rb`
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
include CarrierWave::MiniMagick
#Include the sprockets-rails helper for Rails 4+ compatibility:
include Sprockets::Rails::Helper
storage :fog
version :index_size do
process :resize_to_fill => [258, 173]
end
version :thumb_size do
process :resize_to_fill => [100, 100]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
Your config has the bucket name and the database has the filename. These are the only two pieces of information that are actually required to construct a filename (and it can be done without other API calls). The urls are actually pretty regular, so it is fairly straightforward for the server to do this. Hope that helps!

Generate imagemagick / carrierwave thumbnails when they are requested

I am using the carrierwave gem with Rmagick in a Rails app. I've set up a new version in my uploader file:
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :preview do
process :resize_to_fill => [580, 350]
end
end
Of course, I included rmagick and carrierwave in my gemfile. Now I try to load the preview version of my images in my views:
#product.photos.first.image.preview
This does not give any errors, but loads a broken image. If I copy the url of the image, I get a routing error ('no route matches /path/to_my_imagesfolder/preview_image.png'). If I remove the preview method, the image loads properly.
What can the problem be? I thought maybe it was a permissions issue, but I set the uploads folder with 777 and it still fails.
Any ideas?
EDIT: I realized if I upload the images again the new versions are created. Is it possible to make Rmagick create them when they are requested (like TimThumb does in PHP) Or at least is there any command to batch create all the versions?
There must be a better way than uploading all the images...
You can use .recreate_versions!
For example:
Product.all.each do |product|
product.photos.each do |photo|
photo.recreate_versions!
end
end
I'd just use this: https://github.com/markevans/dragonfly

Resources