Skip to content

Instantly share code, notes, and snippets.

@christhomas
Last active May 13, 2020 07:38
Show Gist options
  • Save christhomas/f01f1de3393d78d73359ab6098b2bdb9 to your computer and use it in GitHub Desktop.
Save christhomas/f01f1de3393d78d73359ab6098b2bdb9 to your computer and use it in GitHub Desktop.
Easy script to convert a video into a GIF. Great for screen-recordings that can be shared in bug tickets
#!/usr/bin/env bash
self=$(basename $0)
[ "$1" = "help" ] && [ ! -f "$1" ] \
&& echo -e "scr_to_gif: v1.0" \
&& echo -e "usage: ${self} <filename.mov> <scale> <speed>\n" \
&& echo -e "scale: - Range 100-9999" \
&& echo -e " - A single number to denote the desired width, the height will be automatic. E.g: 300, 500, 640, 800, 1024, 1280, 1600" \
&& echo -e " - Also both dimensions with numbers within range. Separated by a colon. Example: 100:100, 512,512, 800:600" \
&& echo -e " - One dimension x or y can be -1 meaning it'll be automatic and the other number will be used to calculate a ratio. Example: -1:600, 600:-1\n" \
&& echo -e "speed: - Range 1-99 and used to specify the time delay between frames (lower value means gif animates faster)\n" \
&& exit 1
[ -z "$(command -v ffmpeg)" ] && echo "ffmpeg is not found or not installed, please make sure it's in your path before continuing" && exit 1
[ -z "$(command -v gifsicle)" ] && echo "gifsicle is not found or not installed, please make sure it's in your path before continuing" && exit 1
[ -z "$1" ] && echo "Filename not specified" && exit 1
[ ! -f "$1" ] && echo "File '$1' not found" && exit 1
res="$(echo $2 | grep -E "^(-1|[0-9]{3,4})([:](-1|[0-9]{3,4}))?$")"
[ ! -z "$(echo $res | grep -E '^([0-9]{3,4})$')" ] && res="$res:-1"
[ ! -z "${res}" ] && scale="-vf scale='${res}'"
[ ! -z "$(echo $3 | grep -E '^([0-9]{1,2})$')" ] && speed=$3 || speed=10
output="$1.gif"
echo "Input File: '$1'"
echo "Output File: '${output}'"
echo "Resolution: '${2:-same}'"
echo "Speed: '${speed}'"
echo "Scale: '${scale:-same}'"
ffmpeg -i "$1" ${scale} -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=${speed} > "${output}"
@Abrifq
Copy link

Abrifq commented May 13, 2020

I don't know much about gifsicle but can't we use 4 digit dimensions like 1920x1080? It is possible to just change {3} in regexp to {3,} and two three digit numbers to 2 numbers with 3 or more digits. What do you say?

@christhomas
Copy link
Author

christhomas commented May 13, 2020

The code already allows {3,4} since I'm supposing nobody wants a gif smaller than 100x100 or greater than 9999x9999. I can't see anywhere in the latest version that it uses {3}. This is something I changed yesterday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment