Skip to content

Instantly share code, notes, and snippets.

@mattak
Created April 10, 2014 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattak/10335370 to your computer and use it in GitHub Desktop.
Save mattak/10335370 to your computer and use it in GitHub Desktop.
animation gif from mp4 files
#!/usr/bin/env ruby
def run(cmd)
puts "$ #{cmd}"
`#{cmd}`
end
if ARGV.size < 1 || /.mp4$/i !~ ARGV[0]
puts "usage: agif <something.mp4>"
exit 0
end
VIDEO_FILE = ARGV[0]
GIF_FILE = ARGV[0].sub(/\.mp4$/i, ".gif")
TMP_DIR = ".tmp"
RATE = 4
# 1. clean up
if Dir.exist?(TMP_DIR)
`rm -rf #{TMP_DIR}`
end
`mkdir -p #{TMP_DIR}`
# 2. exec
run "ffmpeg -i #{VIDEO_FILE} -r #{RATE} -f image2 #{TMP_DIR}/%05d.jpg"
# 3. clean up
files=`find #{TMP_DIR} -type f -name '*.jpg' | xargs echo`.chomp
run "convert #{files} #{GIF_FILE}"
`rm -rf #{TMP_DIR}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment