1

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

Felix
  • 5,452
  • 12
  • 68
  • 163

2 Answers2

4

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

mfathy00
  • 1,601
  • 1
  • 13
  • 28
  • This is a borderline [link-only answer](//meta.stackexchange.com/q/8231). You should expand your answer to include as much information here, and use the link only for reference. – Blue Aug 30 '16 at 14:11
1

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

Community
  • 1
  • 1
Albin
  • 2,912
  • 1
  • 21
  • 31