A bash script to convert 128KB gifs to mp4

Published by Tim on Monday December 30, 2024

Last modified on November 2nd, 2025 at 7:29

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!

Bi-Weekly Update

Active patrons in a paid tier get regular updates on new content, lessons and courses. For learners. On Fridays, 4pm CET.

Monthly Newsletter

Personal Reflections on Creative Coding, Design and life with Technology, every first Friday of the month, directly to your inbox.

Related

The Story of 128KB

One day in January 2024, I was lethargically scrolling through my Instagram feed on my laptop. And, as so often […]

A 128KB Export Pipeline

With some help by CoPilot I have coded a Bash script that you can use to convert your animations for […]

Lo-Fi Collage Machine

Click here to login or connect!To view this content, you must be a member of Tim's Patreon at €10 or […]

Diogenes meets Demo Festival

Below is the written version of my talk at DEMO Festival in Amsterdam, January 2025. I’ve also recorded an audio […]

DEMO 2025 – My Submissions

Limitations have always been playing a major role in my creative work; I was only able to develop my best […]

Throwback: My Talk at Demo Festival 2022

The next edition of the DEMO Festival is already approaching and I am currently developing a brand new talk for […]

Powers of Two – 128kb by Lena Weber

20 = 1 21 = 222 = 323 = 824 = 1625 = 3226 = 6427 = 128 … »In […]

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 […]