Class: Lolcommits::AnimatedGif

Inherits:
Object
  • Object
show all
Defined in:
lib/lolcommits/animated_gif.rb

Instance Method Summary collapse

Instance Method Details

#create(video_path:, output_path:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lolcommits/animated_gif.rb', line 5

def create(video_path:, output_path:)
  unless File.exist?(video_path)
    debug "**warning** unable to create animated gif, no video at #{video_path}"
    return
  end

  debug "creating animated gif at #{output_path}"
  system_call "ffmpeg -nostats -v quiet -i \"#{video_path}\" \"#{frames_dir}/%09d.png\" > #{null_string}"

  # use fps to set delay and number of frames to skip (for lower filesized gifs)
  fps   = video_fps(video_path)
  skip  = frame_skip(fps)
  delay = frame_delay(fps, skip)
  debug "animated gif choosing every #{skip} frames with a frame delay of #{delay} (video fps: #{fps})"

  # create the looping animated gif from frames (delete frame files except every #{skip} frame)
  Dir["#{frames_dir}/*.png"].each do |frame_filename|
    basename = File.basename(frame_filename)
    frame_number = basename.split('.').first.to_i
    File.delete(frame_filename) if frame_number % skip != 0
  end

  # convert to animated gif with delay and gif optimisation
  system_call "convert -layers OptimizeTransparency -delay #{delay} -loop 0 \"#{frames_dir}/*.png\" -coalesce \"#{output_path}\""

  # remove tmp frames dir
  FileUtils.rm_rf(frames_dir)
end