Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@michaelosthege
Last active February 28, 2024 22:16
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save michaelosthege/cd3e0c3c556b70a79deba6855deb2cc8 to your computer and use it in GitHub Desktop.
Save michaelosthege/cd3e0c3c556b70a79deba6855deb2cc8 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)
@zhenyuan992
Copy link

What imageio version are you running?
ie run this:

import imageio
print(imageio.__version__)

@PeskyWabbit
Copy link

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

@yokoleia
Copy link

yokoleia commented May 22, 2019

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

@praveenr2998
Copy link

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

Can you share the code please...?

@yokoleia
Copy link

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
Copy link

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
Copy link

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

@michaelosthege
Copy link
Author

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