Skip to content

Instantly share code, notes, and snippets.

@jsundram
Created July 23, 2013 07:28
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 jsundram/6060474 to your computer and use it in GitHub Desktop.
Save jsundram/6060474 to your computer and use it in GitHub Desktop.
Create an mp4 from the files in the input directory given. If there aren't a ton, make an animated gif. Assumes ImageMagick (for animated gif) or mjpegtools and ffmpeg for H264-encoded mp4.
from glob import glob
import sys
from tempfile import NamedTemporaryFile
png_dir = sys.argv[1]
wildcard = os.path.join(png_dir, '*.png')
n = len(glob(wildcard))
if n < 100:
# for short files, animated gifs are preferable
subprocess.call("convert -delay 2 -loop 0 %s output.gif" % wildcard).split(" "), cwd=png_dir)
else:
with NamedTemporaryFile(suffix='.yuv') as f:
# http://wiki.webmproject.org/howtos/convert-png-frames-to-webm-video
subprocess.call(("png2yuv -I p -f 24 -b 0 -n %d -j %%06d.png" % n).split(" "), cwd=png_dir, stdout=f)
subprocess.call(("ffmpeg -i %s -vcodec libx264 -tune animation output_video.mp4" % f.name).split(" "), cwd=png_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment