A bash script to convert 128KB gifs to 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
- Create a new file called script.sh in a new folder
- copy the code above into it
- save the file
- open that folder in your terminal and run
chmod +x ./script.sh
to make it executable - copy the gif into that folder
- 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?
Since 2018, I have published 236 interviews, case studies, and tutorials, along with over 329 lessons in 21 online courses – and there's more to come! If you want to get full access or simply support my work and help keep this platform thriving, please consider supporting me on Patreon. Thank you very much!

Related

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

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

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

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

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

Your 128kb journey starts here! This is a template you can use to start developing your idea within the 128kb […]

One day in early 2024 I started to experiment with a new idea. I wrote down a set of rules […]

Instagram, Twitter, TikTok… All the main platforms that technically have the required features to connect emerging communies for Creative Coding […]