3
4
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
33
34
35
36
37
|
# File 'lib/lolcommits/animated_gif.rb', line 3
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}"
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})"
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
MiniMagick.convert do |convert|
convert << "#{frames_dir}/*.png"
convert.layers "OptimizeTransparency"
convert.delay delay
convert.loop 0
convert.coalesce
convert << output_path
end
FileUtils.rm_rf(frames_dir)
end
|