Created
July 2, 2019 08:00
-
-
Save nakagami/0e8fc1b93776cb870cf188d29708d8f1 to your computer and use it in GitHub Desktop.
mp4 to animation gif converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import imageio | |
""" | |
Convert mp4 to (animation) gif. | |
Reference: http://imageio.readthedocs.io/en/latest/examples.html#convert-a-movie | |
pip install imageio, imageio-ffmpeg | |
""" | |
def mp4_to_gif(inputpath, outputpath, num_images=None): | |
reader = imageio.get_reader(inputpath) | |
fps = reader.get_meta_data()['fps'] | |
with imageio.get_writer(outputpath, fps=fps) as writer: | |
for i, im in enumerate(reader): | |
if i == num_images: | |
break | |
print(i+1) | |
writer.append_data(im) | |
if __name__ == '__main__': | |
if len(sys.argv) < 3: | |
print('Usage : ' + sys.argv[0] + ' src.mp4 dest.gif') | |
sys.exit() | |
src = sys.argv[1] | |
dest = sys.argv[2] | |
if len(sys.argv) > 3: | |
num_images = int(sys.argv[3]) | |
mp4_to_gif(src, dest, num_images) | |
else: | |
mp4_to_gif(src, dest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment