A bash script to convert 128KB gifs to mp4

Published by Tim on Monday December 30, 2024

input.gif
output.mp4

Do you want to share the creations and sketches you’ve developed for the 128KB challenge on Instagram or other social networks? Bummer, they mostly only accept video files. But here’s a fix for this problem: A shell script you can use to convert your gifs to a mp4-file. It loops the gif three times and multiplies the size times ten, while maintaining sharp pixels.

#!/bin/bash

# Check if the input file is provided
if [ $# -ne 1 ]; then
    echo "Usage: $0 <input_gif>"
    exit 1
fi

# Variables
INPUT_GIF="$1"
BASENAME=$(basename "$INPUT_GIF" .gif)
LOOPED_GIF="looped.gif"
SCALED_GIF="scaled.gif"
OUTPUT_MP4="${BASENAME}.mp4"

# Step 1: Create a copy of the GIF, looping it 3 times
echo "Creating a 3-loop GIF..."
gifsicle --loop=3 "$INPUT_GIF" > "$LOOPED_GIF"

# Step 2: Scale the looped GIF to 1280x1280 with sharp edges
echo "Scaling GIF to 1280x1280 with sharp edges..."
convert "$LOOPED_GIF" -filter point -resize 1280x1280 "$SCALED_GIF"

# Step 3: Convert the scaled GIF to MP4 and ensure it loops
echo "Converting GIF to MP4 with looping..."
ffmpeg -stream_loop 2 -i "$SCALED_GIF" -movflags faststart -pix_fmt yuv420p "$OUTPUT_MP4"

# Step 4: Done!
echo "Done! Output file: $OUTPUT_MP4"

How to use it

  1. Create a new file called script.sh in a new folder
  2. copy the code above into it
  3. save the file
  4. open that folder in your terminal and run chmod +x ./script.sh to make it executable
  5. copy the gif into that folder
  6. run the script: ./script.sh

Dependencies

This script depends on ffmpeg, gifsicle and imagemagick. No need to panic, you can install all these packages via homebrew.

Good luck!

Enjoying the content?

I put a lot of love and effort into developing content on Creative Coding. Since 2018, I have published 221 interviews, case studies, and tutorials, along with over 280 lessons in 17 online courses – and there's more to come! If you'd like to support my work and help keep this platform evolving, please consider supporting me on Patreon. Thank you very much!

Speaking Image

Monthly Newsletter

Fresh perspectives circling around Creative Coding, Design and Technology, every first Friday of the month, directly to your inbox.

Related

p5.js Design Tools Directory

Hi! In this post I’ll collect case studies and direct links to tools that people have built with p5.js and […]

A custom Mockup Tool, built with Processing (updated)

For my students at Elisava, I have created a new version of my mockup-tool. You need two different files for […]

Marcus Aurelius Meditations

Since the beginning of the Corona crisis, I have been more and more interested in the history of ancient philosophy. […]

Form follows Music: The Bach-Project

Generative visuals made from the "Prelude in C" by Johann Sebastian Bach.

FFMPEG Snippet Collection

FFMPEG is a very small and handy tool for converting and editing media from the command-line. Not only is it […]

Discoveries in the Public Domain

The internet holds incredible treasures of media, free to use for anyone. This short video-essay gives you insights into the […]