-
-
Save rsms/3b18391fe38d5bd38b44b5e6fd06581e to your computer and use it in GitHub Desktop.
Convert a video to GIF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -e | |
| if [ "$1" = "" ] || [ "$1" = "-h" ] || [ "$1" = "-help" ] || [ "$1" = "--help" ]; then | |
| echo "usage: ${0##*/} <video-input-file> [<gif-output-file>]" >&2 | |
| echo " if <gif-output-file> is not provided, <video-input-file> with" >&2 | |
| echo " filename extension replaced by \".gif\" is used" >&2 | |
| exit 1 | |
| fi | |
| infile=$1 | |
| outfile=$2 | |
| if [ "$outfile" = "" ]; then | |
| outfile=$(echo "$infile" | sed -E 's/\.[^\.]+$/.gif/g') | |
| fi | |
| if [ -e "$outfile" ] && [ -d ~/.Trash ]; then | |
| mv -f "$outfile" ~/.Trash/"$(basename "$outfile")" | |
| fi | |
| if [ -z $TMPDIR ]; then | |
| TMPDIR=/tmp | |
| fi | |
| palette="${TMPDIR}/palette.png" | |
| echo "palette: $palette" | |
| filters="fps=5,unsharp=luma_msize_x=3:luma_msize_y=3:luma_amount=1.0,scale=800:-1:flags=lanczos" | |
| paletteops="stats_mode=diff" | |
| paletteops="" | |
| ffmpeg -v warning -i "$infile" -vf "$filters,palettegen=$paletteops" -y "$palette" | |
| ffmpeg -v warning -i "$infile" -i "$palette" -lavfi "$filters [x]; [x][1:v] paletteuse" -y "$outfile" | |
| if (which gifsicle >/dev/null); then | |
| gifsicle --optimize=3 --output "$outfile" "$outfile" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment