Skip to content

Instantly share code, notes, and snippets.

@Arcensoth
Last active Jan 7, 2022
Embed
What would you like to do?
#!/usr/bin/env python3
#
# gif2mp4.py
# Bulk convert a folder of GIFs into MP4s
#
# Requirements:
# - Python 3.8+
# - pip install MoviePy
#
# Usage:
# python3 gif2mp4.py <path/to/gifs> [optional/path/to/output]
#
# Source:
# https://gist.github.com/Arcensoth/a6051dbab173fda208c3eae226a62b3f
import sys
from pathlib import Path
from moviepy.editor import VideoFileClip
in_dir_name = sys.argv[1]
in_dir_path = Path(in_dir_name).absolute()
out_dir_name = sys.argv[2] if len(sys.argv) > 2 else None
out_dir_path = Path(out_dir_name).absolute() if out_dir_name is not None else in_dir_path
print(f"Converting GIFs in: {in_dir_path}")
print(f" to MP4s in: {out_dir_path}")
for node in in_dir_path.glob('**/*.gif'):
if node.is_file():
print(f"Converting GIF: {node}")
node_out = (out_dir_path / node.name).with_suffix(".mp4")
print(f" to MP4: {node_out}")
with VideoFileClip(str(node)) as clip:
clip.write_videofile(str(node_out))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment