-
Notifications
You must be signed in to change notification settings - Fork 0
/
gif.plugins.bash
executable file
·311 lines (271 loc) · 10.4 KB
/
gif.plugins.bash
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# shellcheck shell=bash
cite about-plugin
about-plugin 'Video to gif, gif to WebM helper functions'
# Based loosely on:
# https://gist.github.com/SlexAxton/4989674#comment-1199058
# https://linustechtips.com/main/topic/343253-tutorial-convert-videogifs-to-webm/
# and other sources
# Renamed gifify to v2gif to go avoid clobbering https://github.com/jclem/gifify
# Requirements (Mac OS X using Homebrew): brew install ffmpeg giflossy imagemagick
# Requirements on Ubuntu: sudo apt install ffmpeg imagemagick ; plus install https://github.com/pornel/giflossy
# Optional: install mediainfo for autodetection of original video FPS.
# Optional: if lossy is not important, Ubuntu has gifsicle packaged for apt-get, instead of giflossy
# Optional: gifski (from `brew install gifski` or github.com/ImageOptim/gifski)
# for high quality huge files.
v2gif () {
about 'Converts a .mov/.avi/.mp4 file into an into an animated GIF.'
group 'GIF'
param '1: MOV/AVI/MP4 file name(s)'
param '2: -w <num> ; Optional: max width in pixels'
param '3: -l <num> ; Optional: extra lossy level for smaller files (80-200 make sense, needs giflossy instead of gifsicle)'
param '4: -h ; Optional: high quality using gifski (installed seperately) - overrides "--lossy" above!'
param '5: -d ; Optional: delete the original video file if succeeded'
param '6: -t ; Optional: Tag the result with quality stamp for comparison use'
param '7: -f <num> ; Optional: Change number of frames per second (default 10 or original FPS if mediainfo installed)'
param '8: -a <num> ; Optional: Alert if resulting file is over <num> kilobytes (default is 5000, 0 turns off)'
param '9: -m ; Optional: Also create a WebM file (will one day replace GIF, Smaller and higher quality than mp4)'
example '$ v2gif foo.mov'
example '$ v2gif foo.mov -w 600'
example '$ v2gif -l 100 -d *.mp4'
example '$ v2gif -dh *.avi'
example '$ v2gif -thw 600 *.avi *.mov'
declare -i convert -i ffmpeg -i mediainfo -i gifsicle -i getopt
convert=$(which convert) ; [[ -x "$convert" ]] || { echo "No convert found!" ; return 2 ;}
ffmpeg=$(which ffmpeg) ; [[ -x "$ffmpeg" ]] || { echo "No ffmpeg found!" ; return 2 ;}
mediainfo=$(which mediainfo) ; [[ -x "$mediainfo" ]] || { echo "No mediainfo found!" ; return 2 ;}
gifsicle=$(which gifsicle) ; [[ -x "$gifsicle" ]] || { echo "No gifsicle found!" ; return 2 ;}
getopt=$(which getopt)
if [[ "$OSTYPE" == "darwin"* ]] ; then
# Getopt on BSD is incompatible with GNU
getopt=/usr/local/opt/gnu-getopt/bin/getopt
[[ -x "$getopt" ]] || { echo "No GNU-getopt found!" ; return 2 ;}
fi
local args
args=$($getopt -l "alert:" -l "lossy:" -l "width:" -l del,delete -l high -l tag -l "fps:" -l webm -o "a:l:w:f:dhmt" -- "$@")
if [[ $? -ne 0 ]]; then
echo 'Terminating...' >&2
return 2
fi
eval set -- "$args"
local use_gifski=""
local opt_del_after=""
local maxsize=""
local lossiness=""
local maxwidthski=""
local giftagopt=""
local giftag=""
local defaultfps=10
local infps=""
local fps=""
local make_webm=""
local alert=5000
while [[ $# -ge 1 ]]; do
case "$1" in
--)
# No more options left.
shift
break
;;
-d|--del|--delete)
# Delete after
opt_del_after="true"
shift
;;
-h|--high)
#High Quality, use gifski
local gifski
gifski=$(which gifski) ; [[ -x "$gifski" ]] || { echo "No gifski found!" ; return 2 ; }
use_gifski=true
giftag="${giftag}-h"
shift
;;
-w|--width)
maxsize="-vf scale=$2:-1"
maxwidthski="-W $2"
giftag="${giftag}-w$2"
shift 2
;;
-t|--tag)
# mark with a quality tag
giftagopt="true"
shift
;;
-l|--lossy)
# Use giflossy parameter
lossiness="--lossy=$2"
giftag="${giftag}-l$2"
shift 2
;;
-f|--fps)
# select fps
infps="$2"
giftag="${giftag}-f$2"
shift 2
;;
-a|--alert)
# set size alert
alert="$2"
shift 2
;;
-m|--webm)
# set size alert
make_webm="true"
shift
;;
esac
done
if [[ -z "$*" ]]; then
echo "$(tput setaf 1)No input files given. Example: v2gif file [file...] [-w <max width (pixels)>] [-l <lossy level>] $(tput sgr 0)"
echo "-d/--del/--delete Delete original vid if done suceessfully (and file not over the size limit)"
echo "-h/--high High Quality - use Gifski instead of gifsicle"
echo "-w/--width N Lock maximum gif width to N pixels, resize if necessary"
echo "-t/--tag Add a tag to the output gif describing the options used (useful for comparing several options)"
echo "-l/--lossy N Use the Giflossy parameter for gifsicle (If your version supports it)"
echo "-f/--fps N Override autodetection of incoming vid FPS (useful for downsampling)"
echo "-a/--alert N Alert if over N kilobytes (Defaults to 5000)"
echo "-m/--webm Also create a webm file"
return 1
fi
# Prepare the quality tag if requested.
[[ -z "$giftag" ]] && giftag="-default"
[[ -z "$giftagopt" ]] && giftag=""
for file ; do
local output_file="${file%.*}${giftag}.gif"
local del_after=$opt_del_after
if [[ "$make_webm" ]] ; then
$ffmpeg -loglevel panic -i "$file" \
-c:v libvpx -crf 4 -threads 0 -an -b:v 2M -auto-alt-ref 0 \
-quality best -loop 0 "${file%.*}.webm" || return 2
fi
# Set FPS to match the video if possible, otherwise fallback to default.
if [[ "$infps" ]] ; then
fps=$infps
else
fps=$defaultfps
if [[ -x $mediainfo ]] ; then
fps=$($mediainfo "$file" | grep "Frame rate " |sed 's/.*: \([0-9.]\+\) .*/\1/' | head -1)
[[ -z "$fps" ]] && fps=$($mediainfo "$file" | grep "Minimum frame rate" |sed 's/.*: \([0-9.]\+\) .*/\1/' | head -1)
fi
fi
echo "$(tput setaf 2)Creating '$output_file' at $fps FPS ...$(tput sgr 0)"
if [[ "$use_gifski" = "true" ]] ; then
# I trust @pornel to do his own resizing optimization choices
$ffmpeg -loglevel panic -i "$file" -r $fps -vcodec png v2gif-tmp-%05d.png && \
$gifski $maxwidthski --fps "$(printf "%.0f" $fps)" -o "$output_file" v2gif-tmp-*.png || return 2
else
$ffmpeg -loglevel panic -i "$file" $maxsize -r $fps -vcodec png v2gif-tmp-%05d.png && \
$convert +dither -layers Optimize v2gif-tmp-*.png GIF:- | \
$gifsicle $lossiness --no-warnings --colors 256 --delay="$(echo "100/$fps"|bc)" --loop --optimize=3 --multifile - > "$output_file" || return 2
fi
rm v2gif-tmp-*.png
# Checking if the file is bigger than Twitter likes and warn
if [[ $alert -gt 0 ]] ; then
local out_size
out_size=$(wc --bytes < "$output_file")
if [[ $out_size -gt $(( alert * 1000 )) ]] ; then
echo "$(tput setaf 3)Warning: '$output_file' is $((out_size/1000))kb.$(tput sgr 0)"
[[ "$del_after" == "true" ]] && echo "$(tput setaf 3)Warning: Keeping '$file' even though --del requested.$(tput sgr 0)"
del_after=""
fi
fi
[[ "$del_after" = "true" ]] && rm "$file"
done
echo "$(tput setaf 2)Done.$(tput sgr 0)"
}
any2webm () {
about 'Converts an movies and Animated GIF files into an into a modern quality WebM video.'
group 'GIF'
param '1: GIF/video file name(s)'
param '2: -s <WxH> ; Optional: set <W>idth and <H>eight in pixels'
param '3: -d ; Optional: delete the original file if succeeded'
param '4: -t ; Optional: Tag the result with quality stamp for comparison use'
param '5: -f <num> ; Optional: Change number of frames per second'
param '6: -b <num> ; Optional: Set Bandwidth (quality/size of resulting file), Defaults to 2M (bits/sec, accepts fractions)"'
param '7: -a <num> ; Optional: Alert if resulting file is over <num> kilobytes (default is 5000, 0 turns off)'
example '$ any2webm foo.gif'
example '$ any2webm *.mov -b 1.5M -s 600x480'
# Parse the options
local args
args=$(getopt -l alert -l "bandwidth:" -l "width:" -l del,delete -l tag -l "fps:" -l webm -o "a:b:w:f:dt" -- "$@")
if [[ $? -ne 0 ]]; then
echo 'Terminating...' >&2
return 2
fi
eval set -- "$args"
local opt_del_after=""
local size=""
local webmtagopt=""
local webmtag=""
local defaultfps=10
local fps=""
local bandwidth="2M"
local alert=5000
while [[ $# -ge 1 ]]; do
case "$1" in
--)
# No more options left.
shift
break
;;
-d|--del|--delete)
# Delete after
opt_del_after="true"
shift
;;
-s|--size)
size="-s $2"
webmtag="${webmtag}-s$2"
shift 2
;;
-t|--tag)
# mark with a quality tag
webmtagopt="true"
shift
;;
-f|--fps)
# select fps
fps="-r $2"
webmtag="${webmtag}-f$2"
shift 2
;;
-b|--bandwidth)
# select bandwidth
bandwidth="$2"
webmtag="${webmtag}-b$2"
shift 2
;;
-a|--alert)
# set size alert
alert="$2"
shift 2
;;
esac
done
if [[ -z "$*" ]]; then
echo "$(tput setaf 1)No input files given. Example: any2webm file [file...] [-w <max width (pixels)>] < $(tput sgr 0)"
return 1
fi
# Prepare the quality tag if requested.
[[ -z "$webmtag" ]] && webmtag="-default"
[[ -z "$webmtagopt" ]] && webmtag=""
for file ; do
local output_file="${file%.*}${webmtag}.webm"
local del_after=$opt_del_after
echo "$(tput setaf 2)Creating '$output_file' ...$(tput sgr 0)"
$ffmpeg -loglevel panic -i "$file" \
-c:v libvpx -crf 4 -threads 0 -an -b:v $bandwidth -auto-alt-ref 0 \
-quality best $fps $size -loop 0 -pix_fmt yuva420p "$output_file" || return 2
# Checking if the file is bigger than Twitter likes and warn
if [[ $alert -gt 0 ]] ; then
local out_size
out_size=$(wc --bytes < "$output_file")
if [[ $out_size -gt $(( alert * 1000 )) ]] ; then
echo "$(tput setaf 3)Warning: '$output_file' is $((out_size/1000))kb.$(tput sgr 0)"
[[ "$del_after" == "true" ]] && echo "$(tput setaf 3)Warning: Keeping '$file' even though --del requested.$(tput sgr 0)"
del_after=""
fi
fi
[[ "$del_after" = "true" ]] && rm "$file"
done
echo "$(tput setaf 2)Done.$(tput sgr 0)"
}