Skip to content

Instantly share code, notes, and snippets.

@pusewicz
Last active December 29, 2015 01:09
Show Gist options
  • Save pusewicz/7590942 to your computer and use it in GitHub Desktop.
Save pusewicz/7590942 to your computer and use it in GitHub Desktop.
Convert you QuickTime recorded screencast in the MOV and turn it into an animated GIF
#!/usr/bin/env ruby
=begin
DEPENDENCIES:
$ brew install ffmpeg
$ brew install imagemagick
=end
require 'optparse'
require 'fileutils'
require 'tmpdir'
VERSION = "0.1"
options = {
fps: 15,
loop: true
}
OptionParser.new do |opts|
opts.banner = "Usage: #{opts.program_name} file.mov file.gif [options]"
opts.on('-v', '--version') do
puts "#{opts.program_name} v#{VERSION}"
exit
end
opts.on('--fps N', Integer, "Set N frames per second (default: #{options[:fps]})") do |fps|
options[:fps] = fps
end
opts.on('-l', '--[no-]loop', "Turn looping on or off (default: #{options[:loop]})") do |lp|
options[:loop] = lp
end
end.parse!
mov_file = File.expand_path(ARGV[0])
gif_file = File.expand_path(ARGV[1])
abort "Please specify both mov and gif file" unless File.exist?(mov_file) && gif_file
puts "Processing: #{mov_file} -> #{gif_file}, #{options.inspect}"
temp_dir = Dir.mktmpdir(File.basename($0))
temp_template = File.join(temp_dir, '%5d.png')
convert_to_png = [
'ffmpeg',
'-loglevel',
'quiet',
'-i',
mov_file,
'-r',
options[:fps].to_s,
temp_template
]
puts "Executing #{convert_to_png.join(' ')}"
Process.waitpid spawn(*convert_to_png)
convert_to_gif = [
'convert',
'-loop',
options[:loop] ? '0' : '1',
'-delay',
"1x#{options[:fps]}",
File.join(temp_dir, '*.png'),
gif_file
]
puts "Executing #{convert_to_gif.join(' ')}"
Process.waitpid spawn(*convert_to_gif)
FileUtils.rm_rf(temp_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment