Skip to content

Instantly share code, notes, and snippets.

@csexton
Created July 3, 2019 17:43
Show Gist options
  • Save csexton/546265b6756a2f0e1c21e6f52bf0f21f to your computer and use it in GitHub Desktop.
Save csexton/546265b6756a2f0e1c21e6f52bf0f21f to your computer and use it in GitHub Desktop.
Simple script to convert a mov file to an animated gif. Requires ffmpeg.
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'optparse'
options = {
rate: 30,
log: 0,
}
OptionParser.new do |opts|
opts.banner = "Usage: mov2gif [options] file"
opts.on("-v", "--verbose [LOG_LEVEL]", "Set logging level") do |v|
options[:log] = v
end
opts.on("-r", "--rate [RATE]", "Set frame rate (Hz value, fraction or abbreviation)") do |v|
options[:rate] = v
end
end.parse!
in_file = ARGV.first
out_file = File.basename(in_file, ".*" ) + ".gif"
unless File.file?(in_file)
STDERR.puts "ERROR: #{in_file} does not exist"
exit 1
end
cmd = %[ffmpeg -i "#{in_file}" -vf "scale='min(1280,iw)':-1" -pix_fmt rgb8 -r #{options[:rate]} -f gif -v #{options[:log]} -stats #{out_file}]
puts "Converting #{in_file} to #{out_file}"
puts "--> #{cmd}"
system cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment