Skip to content

Instantly share code, notes, and snippets.

@dhoko
Last active April 15, 2019 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhoko/2a57b9b43a333c568973c26c2926d84f to your computer and use it in GitHub Desktop.
Save dhoko/2a57b9b43a333c568973c26c2926d84f to your computer and use it in GitHub Desktop.
Convert a video to gif (webm/mp4)
#!/bin/bash
#
inputFile=$1
FPS=15
WIDTH=$2
OUTPUT='output.gif';
PALETTE_OUTPUT='tmp_palette.png';
#
# Convert mp4/webm to gif original or custom size
# Usage:
# ./convert.sh <fileName>.<ext> <size>
# Output file = ./output.gif
#
#
if [[ "$inputFile" == *.webm ]]
then
ffmpeg -threads 4 -y -i "$inputFile" -vf palettegen "$PALETTE_OUTPUT"
ffmpeg -threads 4 -y -i "$inputFile" -i "$PALETTE_OUTPUT" -filter_complex "fps=$FPS,scale=$WIDTH:-1:flags=lanczos[x];[x][1:v]paletteuse" -r 10 $OUTPUT
else
#Generate palette for better quality
ffmpeg -threads 4 -i "$inputFile" -vf fps=$FPS,scale=$WIDTH:-1:flags=lanczos,palettegen "$PALETTE_OUTPUT" -threads 4
#Generate gif using palette
ffmpeg -threads 4 -i "$inputFile" -i "$PALETTE_OUTPUT" -loop 0 -filter_complex "fps=$FPS,scale=$WIDTH:-1:flags=lanczos[x];[x][1:v]paletteuse" $OUTPUT
fi;
rm "$PALETTE_OUTPUT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment