class ScreenshotGenerator
Constants
- VERSION
Attributes
size[R]
video[R]
Public Class Methods
extract_frame(video, *args)
click to toggle source
Helper method for fetching a single frame
# File lib/screenshot_generator.rb, line 5 def self.extract_frame(video, *args) new(video).extract_frame(*args) end
extract_multi(video, *args)
click to toggle source
# File lib/screenshot_generator.rb, line 9 def self.extract_multi(video, *args) new(video).extract_multi(*args) end
new(video, size: '640x400')
click to toggle source
# File lib/screenshot_generator.rb, line 13 def initialize(video, size: '640x400') @video = video @size = size raise ArgumentError, "Video does not exist" unless File.exist? @video end
Protected Class Methods
ffmpeg()
click to toggle source
# File lib/screenshot_generator.rb, line 58 def self.ffmpeg @ffmpeg ||= `which ffmpeg`.chomp end
Public Instance Methods
extract_frame(offset, path)
click to toggle source
# File lib/screenshot_generator.rb, line 21 def extract_frame(offset, path) raise ArgumentError, "File already exists" if File.exist? path offset = format_offset(offset) ffmpeg( "-loglevel", "error", "-ss", offset, "-i", video, "-frames:v", 1, "-s", size, path ) end
extract_multi(dir, count)
click to toggle source
# File lib/screenshot_generator.rb, line 35 def extract_multi(dir, count) dir = Pathname.new(dir) 0.upto(count) do |index| path = dir + "#{index}.jpg" segment_size = length / (count + 2) offset = (index + 1) * segment_size extract_frame(offset, path) end end
length()
click to toggle source
# File lib/screenshot_generator.rb, line 45 def length @length ||= get_length end
Protected Instance Methods
ffmpeg(*args)
click to toggle source
# File lib/screenshot_generator.rb, line 51 def ffmpeg(*args) options = [self.class.ffmpeg, *args].map(&:to_s) options.push(:err=>[:child, :out]) IO.popen(options) {|io| return io.read } raise "ffmpeg comamnd #{options.inspect} failed" unless $?.success? end
format_offset(v)
click to toggle source
# File lib/screenshot_generator.rb, line 72 def format_offset(v) m, s = v.divmod(60) h, m = m.divmod(60) "%02i:%02i:%02i" % [h, m, s] end
get_length()
click to toggle source
# File lib/screenshot_generator.rb, line 62 def get_length output = ffmpeg("-i", video, "-frames", 1) if output =~ /Duration:\s*([0-9:.]+)/ h,m,s = $1.split(":").map(&:to_i) h*60^2 + m*60 + s else 60 end end