-
Notifications
You must be signed in to change notification settings - Fork 5
/
mkgif
executable file
·72 lines (62 loc) · 2.04 KB
/
mkgif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
set -e
PROG=${0##*/}
pushd "$(dirname "$0")/.." >/dev/null
popd >/dev/null
_err() { echo "$PROG: $@" >&2; exit 1; }
FPS=50
MAXSIZE=800
MAXCOLORS=64
INFILE=
OUTFILE=
DITHER=false
while [[ $# -gt 0 ]]; do case "$1" in
-h|-help|--help) cat << EOF
Make a GIF from a video
usage: $PROG [options] <videofile>
options:
-fps=FPS Set FPS limit. Default: $FPS
-size=SIZE Set size limit. Default: $MAXSIZE
-colors=N Set color limit. Default: $MAXCOLORS
-d[ither] Enable dithering
-o FILE, -output=FILE
Write output to FILE instead of <videofile>.gif
-h, -help Print help on stdout and exit
EOF
exit ;;
-fps=*) FPS=${1:5}; shift ;;
-size=*) MAXSIZE=${1:6}; shift ;;
-colors=*) MAXCOLORS=${1:8}; shift ;;
-output=*) OUTFILE=${1:8}; shift ;;
-d|-dither) DITHER=true; shift ;;
-o) [ -z "$2" ] && _err "missing value for $1"; OUTFILE=$2; shift; shift ;;
-*) _err "unknown option $1" ;;
*) [ -n "$INFILE" ] && _err "unexpected argument $1"; INFILE=$1; shift ;;
esac; done
[ -z "$INFILE" ] && _err "missing input file"
OUTFILE=${OUTFILE:-$INFILE.gif}
rm -rf "$OUTFILE"
FFMPEG_VF="fps=$FPS"
# FFMPEG_VF="${FFMPEG_VF},format=rgb24"
# FFMPEG_VF="${FFMPEG_VF},colorspace=all=bt709:trc=srgb:format=yuv420p"
FFMPEG_VF="${FFMPEG_VF},scale=$MAXSIZE:-1:flags=lanczos"
# FFMPEG_VF="${FFMPEG_VF},pp=al" # fix whites, see ffmpeg.org/ffmpeg-filters.html#pp
FFMPEG_VF="${FFMPEG_VF},split[s0][s1];[s0]palettegen=stats_mode=full:max_colors=$MAXCOLORS[p]"
if $DITHER; then
FFMPEG_VF="${FFMPEG_VF};[s1][p]paletteuse=dither=bayer"
else
FFMPEG_VF="${FFMPEG_VF};[s1][p]paletteuse"
fi
ffmpeg \
-i "$INFILE" \
-vf "$FFMPEG_VF" \
-loop 0 \
"$OUTFILE"
/bin/ls -lhF "$OUTFILE" | awk '{print $5}'
# ffmpeg -i "$INFILE" -vf "fps=20,scale=800:-1:flags=lanczos" -c:v pam -f image2pipe - \
# | convert - -loop 0 -layers optimize "$OUTFILE"
# ffmpeg \
# -i "$INFILE" \
# -vf "fps=20,scale=800:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" \
# -loop 0 \
# "$OUTFILE"