Skip to content

Instantly share code, notes, and snippets.

@villares
Last active July 31, 2023 06:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save villares/9105ff20c365f853d2672aced4ac80d9 to your computer and use it in GitHub Desktop.
Save villares/9105ff20c365f853d2672aced4ac80d9 to your computer and use it in GitHub Desktop.
A bare bones mp4 to gif converter Python CLI script
#! /home/villares/thonny-python-env/bin/python3
"""
MP4 to GIF animation. Depends on moviepy and ffmpeg.
"""
import argparse
from moviepy.editor import *
parser = argparse.ArgumentParser(prog='Create a GIF animation from a MP4 file. Depends on moviepy and ffmpeg.')
parser.add_argument('-i', '--input', help='Input .mp4 file.')
parser.add_argument('-o', '--output', default='output.gif', help='Optional output file name. The default is "output.gif"')
parser.add_argument('-r', '--fps', default=10, type=int, help='To chage frame rate. The default is 10 FPS')
args = parser.parse_args()
if str(args.input).lower().endswith('.mp4'):
try:
my_clip = VideoFileClip(args.input) #.subclip(0,2)
result = CompositeVideoClip([my_clip])
result.write_gif(args.output, fps=args.fps, program='ffmpeg')
except Exception as error:
print(error)
elif args.input is None:
print('No input file provided. Use -h for usage help.')
else:
print(f'{args.input}\nis not a valid input file.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment