Skip to content

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.

Copy link

@Joesauris 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.

Copy link

@zhenyuan992 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.

Copy link

@PeskyWabbit PeskyWabbit commented May 21, 2018

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

@yokoleia

This comment has been minimized.

Copy link

@yokoleia yokoleia commented May 22, 2019

I wrote a multi-threaded version of this which increases efficiency by converting multiple files at once.

@praveenr2998

This comment has been minimized.

Copy link

@praveenr2998 praveenr2998 commented Aug 14, 2020

I wrote a multi-threaded version of this which increases efficiency by converting multiple files at once.

Can you share the code please...?

@yokoleia

This comment has been minimized.

Copy link

@yokoleia yokoleia commented Aug 15, 2020

the code is a gist on my account. This was a quick project that i haven't looked back at so be careful and read through it before you use it.
also, just fyi, it's set up to convert mp4 to gif, but mp4 actually is much more efficient than gif.

@64qam

This comment has been minimized.

Copy link

@64qam 64qam commented Oct 24, 2020

It does not convert at all, just the first frame of the video. Then for the rest of the duration of the original video, there is only a still frame. Python 3.8,1, imageio 2.9 Thanks.

@abhishekgoyal-a11y

This comment has been minimized.

Copy link

@abhishekgoyal-a11y abhishekgoyal-a11y commented May 20, 2021

How to Convert Video to GIF Using Python
Visit:- https://www.techroadmap.in/blog/how-to-convert-video-to-gif-using-python/

@michaelosthege

This comment has been minimized.

Copy link
Owner Author

@michaelosthege michaelosthege commented May 20, 2021

How to Convert Video to GIF Using Python
Visit:- https://www.techroadmap.in/blog/how-to-convert-video-to-gif-using-python/

Under the hood all the GIF-writing methods use PIL.
The link above does it too, but falls short on the quality of the output. Here's some troubleshooting insight I earned painfully a few days ago:

Problem 1: Transparency of frames

Symptom: Output GIF has bad quality on texts. Looks like really bad antialiasing.

Reason: The alpha channel of frames is just cut away by pillow, but unless there wasn't any transparency this is not the correct way to remove transparency.

Solution: Make sure to create frames (e.g. matplotlib figures) with white background so cutting away the alpha channel doesn't break the colors.

Problem 2: Grizzly output GIFs

Symptom: Output GIF has artifacts, even though the frames didn't. (Like in the example from the link above.)

Reason: ???

Solution: Try to call .quantize() on the PIL.Image frames.

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