49 lines
1.2 KiB
Ruby
Executable File
49 lines
1.2 KiB
Ruby
Executable File
#!/usr/bin/env ruby
|
|
# - encoding: utf-8 -
|
|
#
|
|
# Intro (in German):
|
|
# https://depone.net/2016/03/01/video-in-animiertes-gif-konvertieren/
|
|
#
|
|
# Hat tip for the palette trick to:
|
|
# http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
|
|
|
|
require 'tmpdir'
|
|
|
|
DEFAULT_WIDTH=400
|
|
DEFAULT_FPS=10
|
|
name = ARGV[0]
|
|
|
|
if ARGV.length == 0 or name == '--help'
|
|
puts <<EOL
|
|
usage: convert-to-gif <filename> [<width>] [<fps>]
|
|
|
|
Supply a video file that should be converted as <filename> and resize
|
|
the resulting GIF to <width> with <fps> frames per second. Width and
|
|
frames per second are optional parameters.
|
|
|
|
$ convert-to-gif video.mp4
|
|
|
|
Will result in a GIF with #{DEFAULT_FPS} fps that is #{DEFAULT_WIDTH}
|
|
pixel wide.
|
|
EOL
|
|
exit
|
|
end
|
|
|
|
|
|
Dir.mktmpdir do |tmpdir|
|
|
palette = File.join(tmpdir, "palette.png")
|
|
|
|
unless File.exist?(name)
|
|
abort "File [#{name}] does not exist. Please see --help."
|
|
end
|
|
|
|
width = ARGV[1] || DEFAULT_WIDTH
|
|
fps = ARGV[2] || DEFAULT_FPS
|
|
|
|
basename = File.basename(name, ".*")
|
|
filters="fps=#{fps},scale=#{width}:-1:flags=lanczos"
|
|
|
|
system("ffmpeg -i #{name} -vf '#{filters},palettegen' -y #{palette}")
|
|
system("ffmpeg -i #{name} -i #{palette} -lavfi '#{filters} [x]; [x][1:v] paletteuse' -y #{basename}.gif")
|
|
end
|