Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)
@Joesauris

This comment has been minimized.

Joesauris commented Feb 8, 2018

I've been stuck on this error code...

AttributeError: module 'imageio' has no attribute 'get_reader'

Any suggestions on why? I'm new to python so it is likely a problem with me not the code I expect. But I'd really like it to work so I figured who better to ask than you?

When I run:

help(imageio.get_reader)
It prints documentation on get_reader, so I know python sees it, but don't know why it can't find it in the script

@zhenyuan992

This comment has been minimized.

zhenyuan992 commented Feb 26, 2018

What imageio version are you running?
ie run this:

import imageio
print(imageio.__version__)
@PeskyWabbit

This comment has been minimized.

PeskyWabbit commented May 21, 2018

whenever I use this method it does not convert the entire mp4 but only part of it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment