Skip to content

Instantly share code, notes, and snippets.

@robinsloan
Last active May 21, 2016 04:26
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 robinsloan/d64e03684efcfc2dae4f994f6f2b7bea to your computer and use it in GitHub Desktop.
Save robinsloan/d64e03684efcfc2dae4f994f6f2b7bea to your computer and use it in GitHub Desktop.
Very brittle, but maybe useful
# for when you have a bunch of images in a dir that are not *quite*
# numbered the way ffmpeg wants them to be, e.g.
# image-1, image-2, ... image-10, image-11, ... image-100, image-101
if ARGV.length < 1
puts "Usage: ruby makevideo.rb name-of-directory-with-all-your-dang-images-in-it"
exit
end
dir = ARGV[0]
unless File.directory?(dir)
puts "#{dir} isn't a directory :("
exit
end
# assumes png -- you can change it, but be sure to change it everywhere
filenames = Dir["#{dir}/*.png"]
out_dir = "#{dir}-renumbered"
`mkdir #{out_dir}`
num_digits = 3 # increase this if you have > 999 images...!
print "Renumbering #{filenames.length} files: "
filenames.each do |filename|
# this expects <something>-2.png, <something>-2.png, etc.
# if there aren't hyphens preceding your numbers, e.g.
# pic1.png, pic2.png, pic3.png
# you could substitute a regex like
# /pic(\d+)\.png/
num = filename.match(/-(\d+)\.png/)[1].rjust(num_digits, "0")
print "#{num}... "
`cp #{filename} #{out_dir}/renumbered-#{num}.png`
end
puts ""
out_video = "output-#{dir}.mp4"
puts "Making video"
`cd #{out_dir}; ffmpeg -y -r 24 -i "renumbered-%0#{num_digits}d.png" -c:v libx264 -crf 18 -preset slow -pix_fmt yuv420p #{out_video}`
puts "Okay! Your video is in #{out_dir}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment