Skip to content

Instantly share code, notes, and snippets.

@alskipp
Created July 29, 2020 16:14
Show Gist options
  • Save alskipp/f2acd3987cf758d38510a55c537dfa9a to your computer and use it in GitHub Desktop.
Save alskipp/f2acd3987cf758d38510a55c537dfa9a to your computer and use it in GitHub Desktop.
Mangle your .mp4 files into .gif
#! /usr/bin/env nix-shell
#! nix-shell -i bash -p ffmpeg gifsicle
scale="${3:-320}"
fps="${4:-15}"
palette="/tmp/palette.png"
filters="fps=$fps,scale=$scale:-1:flags=lanczos"
ffmpeg -v warning -i "$1" -vf "$filters,palettegen=stats_mode=diff" -y $palette
ffmpeg -i "$1" -i $palette -lavfi "$filters,paletteuse=dither=bayer:bayer_scale=5:diff_mode=rectangle" -y -f gif - \
| gifsicle --optimize=3 --delay=3 > "$2"
@alskipp
Copy link
Author

alskipp commented Jul 29, 2020

If you use Nix, then dependencies will be automatically installed, otherwise ensure you have ffmpeg and gifsicle installed.

Basic usage (with default pixel width 320 & fps 15):

vid_to_gif input.mp4 output.gif

Specifying pixel width and frames per second:

vid_to_gif input.mp4 output.gif 600 30

Cobbled together from:
https://cassidy.codes/blog/2017/04/25/ffmpeg-frames-to-gif-optimization/
https://dev.to/piczmar_0/quickly-make-a-gif-from-a-video-553m

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