Created
October 14, 2024 14:18
-
-
Save arachonteur/3f45581b8f232b5e83820f98160d8ee1 to your computer and use it in GitHub Desktop.
Python script for processing a directory of frame sequences, and using Gifski to turn them into GIFs. Recurses nicely, exports consistently.
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
import os | |
import subprocess | |
import sys | |
import shutil | |
# CHANGE THESE TO YOUR FRAME SEQUENCES AND OUTPUT FOLDER | |
base_input_folder = "frame sequences" | |
output_folder = "gifs" | |
def pngs_to_gif(input_folder, output_folder, fps=15): | |
"""Convert PNGs from input_folder into a GIF and save it to output_folder.""" | |
if not shutil.which("gifski"): | |
print("gifski isn't installed. do that first") | |
sys.exit(1) | |
png_files = sorted( | |
[os.path.join(input_folder, f) for f in os.listdir(input_folder) if f.endswith('.png')] | |
) | |
if not png_files: | |
print(f"no pngs found in {input_folder}. skipping") | |
return | |
relative_path = os.path.relpath(input_folder, start=base_input_folder) | |
gif_name = relative_path.replace(os.sep, "_") + ".gif" | |
output_gif = os.path.join(output_folder, gif_name) | |
if os.path.exists(output_gif): | |
print(f"skipping {input_folder}, {gif_name} already exists") | |
return | |
os.makedirs(output_folder, exist_ok=True) | |
command = ["gifski", "--fps", str(fps), "--output", output_gif] + png_files | |
try: | |
subprocess.run(command, check=True) | |
print(f"Created GIF: {output_gif}") | |
except subprocess.CalledProcessError as e: | |
print(f"failed to create gif at {input_folder}. {e}") | |
def process_directories(base_input_folder, output_folder, fps=20): | |
"""Recursively process all subdirectories to create GIFs.""" | |
for root, _, _ in os.walk(base_input_folder): | |
print(f"processing folder: {root}") | |
pngs_to_gif(root, output_folder, fps) | |
if __name__ == "__main__": | |
if not base_input_folder or not output_folder: | |
print("set your base_input_folder and output_folder variables") | |
sys.exit(1) | |
fps = int(input("FPS (default 20): ") or 20) | |
os.makedirs(output_folder, exist_ok=True) | |
process_directories(base_input_folder, output_folder, fps) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment