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?
I put a lot of love and effort into developing content on Creative Coding. Since 2018, I have published 230 interviews, case studies, and tutorials, along with over 321 lessons in 20 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!

Related

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

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

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

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

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

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