Skip to content

Instantly share code, notes, and snippets.

@abhimanyu1289
Forked from michaelosthege/convert.py
Created July 4, 2017 10:38
Show Gist options
  • Save abhimanyu1289/3489816cc87bd0bae71e174a66482923 to your computer and use it in GitHub Desktop.
Save abhimanyu1289/3489816cc87bd0bae71e174a66482923 to your computer and use it in GitHub Desktop.
Convert MP4/AVI clips to GIF with a single Python function
import imageio
import os, sys
class TargetFormat(object):
GIF = ".gif"
MP4 = ".mp4"
AVI = ".avi"
def convertFile(inputpath, targetFormat):
"""Reference: http://imageio.readthedocs.io/en/latest/examples.html#convert-a-movie"""
outputpath = os.path.splitext(inputpath)[0] + targetFormat
print("converting\r\n\t{0}\r\nto\r\n\t{1}".format(inputpath, outputpath))
reader = imageio.get_reader(inputpath)
fps = reader.get_meta_data()['fps']
writer = imageio.get_writer(outputpath, fps=fps)
for i,im in enumerate(reader):
sys.stdout.write("\rframe {0}".format(i))
sys.stdout.flush()
writer.append_data(im)
print("\r\nFinalizing...")
writer.close()
print("Done.")
convertFile("C:\\Users\\Yoda\\Desktop\\EwokPr0n.mp4", TargetFormat.GIF)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment