Felix
Felix

Reputation: 5629

Rails using ffmpeg to make a screenshot of a video

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

Upvotes: 1

Views: 2831

Answers (2)

mfathy00
mfathy00

Reputation: 1611

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

Upvotes: 4

Albin
Albin

Reputation: 3012

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

Upvotes: 1

Related Questions