forked from lukechilds/gifgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gifgen
executable file
·95 lines (87 loc) · 2.23 KB
/
gifgen
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
# Echo help/usage message
show_help() {
echo "gifgen 1.2.0"
echo
echo "Usage: gifgen [options] [input]"
echo
echo "Options:"
echo " -o Output file [input.gif]"
echo " -f Frames per second [10]"
echo " -s Optimize for static background"
echo " -v Display verbose output from ffmpeg"
echo " -w Scale output with horizontal resolution"
echo " -b Begin the clip at a given timestamp (in seconds)"
echo " -d Duration in seconds of the resulting gif, can be combined with at"
echo
echo "Examples:"
echo " $ gifgen video.mp4"
echo " $ gifgen -o demo.gif SCM_1457.mp4"
echo " $ gifgen -sf 15 screencap.mov"
echo " $ gifgen -sf 15 -w 320 screencap.mov"
echo
echo "Begin at 3.5 seconds into the video, make the gif using the next 5.5 seconds"
echo " $ gifgen -b 3.5 -d 5.5 screencap.mov"
}
# Setup defaults
pid=$$
palette="/tmp/gif-palette-$pid.png"
fps="10"
verbosity="warning"
stats_mode="full"
dither="sierra2_4a"
scale=""
begin=""
duration=""
# Parse args
while getopts "hi:o:f:w:b:d:sv" opt; do
case "$opt" in
h)
show_help=true
;;
o)
output=$OPTARG
;;
f)
fps=$OPTARG
;;
s)
stats_mode="diff"
dither="none"
;;
w)
scale=",scale=$OPTARG:-1:flags=lanczos"
;;
b)
begin="-ss $OPTARG"
;;
d)
duration="-t $OPTARG"
;;
v)
verbosity="info"
;;
esac
done
shift "$((OPTIND-1))"
# Grab input file from end of command
input=$1
# Show help and exit if we have no input
[[ "$input" = "" ]] || [[ $show_help = true ]] && show_help && exit
# Check for ffmpeg before encoding
type ffmpeg >/dev/null 2>&1 || {
echo "Error: gifgen requires ffmpeg to be installed"
exit 1
}
# Set output if not specified
if [[ "$output" = "" ]]; then
input_filename=${input##*/}
output=${input_filename%.*}.gif
fi
# Encode GIF
echo "Generating palette..."
ffmpeg -v "$verbosity" $begin $duration -i "$input" -vf "fps=$fps$scale,palettegen=stats_mode=$stats_mode" -y "$palette"
[[ "$verbosity" = "info" ]] && echo
echo "Encoding GIF..."
ffmpeg -v "$verbosity" $begin $duration -i "$input" -i "$palette" -lavfi "fps=$fps$scale [x]; [x][1:v] paletteuse=dither=$dither" -y "$output"
echo "Done!"