-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovie2gif
executable file
·301 lines (229 loc) · 5.22 KB
/
movie2gif
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
#!/bin/sh
# movie2gif
# Convert a video file into an animated GIF file.
VERSION='1.0.1'
movie2gif_main() {
scriptpath="$( realpath "$0" )"
scriptname="$( basename "$scriptpath" )"
e_args=16
e_file=17
DRYRUN=0
VERBOSE=0
OPTIND=
while getopts "a:c:d:f:hm:no:p:r:s:t:vVw:" opt
do
case "$opt" in
a) scale="$OPTARG" ;;
c) colors="$OPTARG" ;;
d) duration="$OPTARG" ;;
f) filters="$OPTARG" ;;
h) movie2gif_help && return ;;
m) more="$OPTARG" ;;
n) DRYRUN=1 ;;
o) crop="$OPTARG" ;;
p) palette="$OPTARG" ;;
r) fps="$OPTARG" ;;
s) start="$OPTARG" ;;
t) height="$OPTARG" ;;
v) movie2gif_version && return ;;
V) VERBOSE=1 ;;
w) width="$OPTARG" ;;
*) _invalid_opt ;;
esac
done
shift "$(( OPTIND - 1 ))"
[ -z "$cmd" ] && cmd="convert"
case "$cmd" in
convert|help) "movie2gif_$cmd" "$@" ;;
*) _invalid_cmd ;;
esac
return $?
}
movie2gif_convert() {
shift "$(( OPTIND - 1 ))"
[ -z "$colors" ] && colors=32
[ -z "$fps" ] && fps=10
[ -z "$height" ] && height=-1
[ -z "$width" ] && width=720
args=
preargs=
if _is_verbose
then
_add_prearg '-loglevel info'
else
_add_prearg '-hide_banner'
_add_prearg '-nostats'
_add_prearg '-loglevel fatal'
fi
if [ -z "$filters" ]
then
filters=
[ -n "$crop" ] && _add_filter "crop=$crop"
[ -n "$scale" ] && _add_filter "setpts=$scale*PTS"
_add_filter "fps=$fps,scale=$width:$height:flags=lanczos"
[ -n "$more" ] && _add_filter "$more"
fi
[ -n "$start" ] && _add_prearg "-ss $start"
[ -n "$duration" ] && _add_prearg "-t $duration"
[ $# -lt 1 ] && _error "Must set input filename." && return $e_args
input="$1"
shift
output="$1"
shift
args="$args $*"
medial="$input"
if [ -z "$palette" ]
then
template="$( mktemp "$medial-palette-$colors.XXXXXX" )"
rm "$template"
palette="$( mktemp "$template.png" )"
tmppalette=1
else
tmppalette=0
fi
[ -z "$output" ] && output="$( echo "$input" | sed 's/\.[^\.]*$//g' ).gif"
if _file_absent "$input"
then
_error "File not found: $input"
return $e_file
fi
_echo "$input => $output"
"${scriptname}_file" "$input"
if _is_verbose
then
_echo "ffmpeg filters: $filters"
_echo "ffmpeg preargs: $preargs"
_echo "ffmpeg args: $args"
fi
_convert
"${scriptname}_file" "$output"
[ $tmppalette -eq 1 ] && rm "$palette"
}
movie2gif_file() {
OPTIND=
while getopts "s" OPTION
do
case "$OPTION" in
s) format='%f %W %H %b' ;;
*) _invalid_opt "$OPTION" ;;
esac
done
shift $(( OPTIND - 1 ))
[ -z "$format" ] && format='%f: %Wx%Hpx (%b)'
[ $# -lt 1 ] && _error "Must set input filename." && return "$ERR_ARG"
format="$format\n"
input="$1"
shift
if _file_absent "$input"
then
_error "File not found: $input"
return "$ERR_FILE"
fi
identify -format "$format" "$input"[0] 2>/dev/null
result=$?
if [ $result -gt 0 ]
then
_error "Not an image or not a format ImageMagick can recognise."
fi
return $?
}
movie2gif_help() {
cat <<HELP
$scriptname $VERSION
USAGE: $scriptname [<options>] <input> [<output>]
Convert a video file into an animated GIF file.
OPTIONS:
-a Time scale. Less than 1 to speed up, more to slow down. (Default: 1)
-c Number of colours in palette. (Default: 32)
-d Duration in seconds. (Default: all)
-f Override all filters with specified ffmpeg filters.
-h Show this help screen.
-m Append specified ffmpeg filters.
-n Dry run. Don't write to any file.
-o Crop image (out_w:out_h:x:y). (Default: no crop)
-r Number of frames per second (FPS). (Default: 10)
-s Start time in seconds. (Default: 0)
-t Image height of output. (Default: -1, keeping ratio with width)
-v Show version.
-V Make console output verbose.
-w Image width of output. (Default: 720)
HELP
}
movie2gif_version() {
echo "$scriptname $VERSION"
}
_add_prearg() {
[ -n "$preargs" ] && preargs="$preargs "
preargs="${preargs}$*"
}
_add_arg() {
[ -n "$args" ] && args="$args "
args="${args}$*"
}
_add_filter() {
[ -n "$filters" ] && filters="$filters,"
filters="${filters}$*"
}
_check_palette() {
_file_exists "$palette" && return
_error "Palette file not found: $palette"
return "$ERR_FILE"
}
_convert() {
# http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
if [ "$tmppalette" -eq 1 ] || ! _file_exists "$palette"
then
_run "ffmpeg $preargs -i \"$input\" -vf \"$filters,palettegen\"" \
"-y $args \"$palette\""
fi
_check_palette || return $?
_run "mogrify +dither -colors $colors \"$palette\""
_run "ffmpeg $preargs -i \"$medial\" -i \"$palette\"" \
"-lavfi \"$filters [x]; [x][1:v] paletteuse\" -y $args \"$output\""
image_optim="$( which image_optim 2>/dev/null )"
[ -n "$image_optim" ] && $image_optim "$output" 1>/dev/null 2>&1
}
_echo() {
echo "$@"
}
_error() {
_echo "$@" >&2
}
_fatal() {
exit_code="$1"
shift
_error "$@"
exit "$exit_code"
}
_file_absent() {
_is_dryrun && return 1
[ ! -f "$1" ]
}
_file_exists() {
_is_dryrun && return
[ -f "$1" ]
}
_invalid_cmd() {
_error "Invalid command: $cmd"
_echo
movie2gif_help
exit $e_args
}
_invalid_opt() {
_error "Invalid option: $opt"
_echo
movie2gif_help
exit $e_args
}
_is_dryrun() {
[ $DRYRUN -gt 0 ]
}
_is_verbose() {
[ $VERBOSE -gt 0 ]
}
_run() {
_is_verbose && _echo "> $*"
_is_dryrun && return
eval "$*"
}
movie2gif_main "$@"