-
Notifications
You must be signed in to change notification settings - Fork 2
/
gifify
executable file
·317 lines (284 loc) · 8.59 KB
/
gifify
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
312
313
314
315
316
317
#!/bin/bash
# SPDX-FileCopyrightText: 2015 - 2024 sudorook <daemon@nullcodon.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -Eeuo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command bc ffmpeg ffprobe sed && exit 3
#
# Define functions
#
function print_usage() {
show_header "Usage:"
cat << EOF
-i --input input video (e.g. input.mp4)
-o --output output GIF (e.g. output.gif)
-s --start starting timestamp (e.g. 01:20.5)
-l --length length in seconds (e.g. 3.5)
-d --dimensions output dimensions, comma-delimited (e.g. 1280x720)
-p --fps GIF output FPS (e.g. 30)
-t --subtitle embed subtitles? (yes, no, both)
EOF
}
function scan_input {
local vstring
local sstring
fstring="$(ffprobe -v error -show_format file:"${IN}")"
vstring="$(ffprobe -v error \
-select_streams v:0 -show_streams file:"${IN}")"
sstring="$(ffprobe -v error \
-select_streams s -show_streams -of flat file:"${IN}")"
DURATION="$(echo "${fstring}" | sed -n "s/^duration=\(.*\)$/\1/p")"
VCODEC="$(echo "${vstring}" | sed -n "s/^codec_name=\(.*\)$/\1/p")"
WIDTH="$(echo "${vstring}" | sed -n "s/^width=\(.*\)$/\1/p")"
HEIGHT="$(echo "${vstring}" | sed -n "s/^height=\(.*\)$/\1/p")"
if ! [[ -v DIMENSIONS ]]; then
DIMENSIONS=("${WIDTH}x${HEIGHT}")
fi
ASPECTRATIO="$(echo "${vstring}" | sed -n "s/^display_aspect_ratio=\(.*\)$/\1/p")"
SIDX="$(echo "${sstring}" | sed -n "s/^streams\.stream.\([0-9]\+\)\.disposition.default=1$/\1/p")"
SIDX="${SIDX:-0}"
SCODEC="$(echo "${sstring}" | sed -n "s/^streams.stream.${SIDX}.codec_name=\"\(.*\)\"$/\1/p")"
}
function make_image_subs {
local in="${IN}"
local dim="${3}"
local width
local height
local out
local cmd
# compute the width and height for the image
if [[ "${dim}" = native ]]; then
width="${WIDTH}"
height="${HEIGHT}"
scale=
else
width="$(echo "${dim}" | sed -n "s,^\([0-9]*\)[:x]\([0-9]*\)$,\1,p")"
height="$(echo "${dim}" | sed -n "s,^\([0-9]*\)[:x]\([0-9]*\)$,\2,p")"
if [ -z "${width}" ] && [ -n "${height}" ]; then
scale=",scale=-1:${height}:flags=lanczos"
width="$(printf %.0f "$(echo "${height} * ${ASPECTRATIO}" | sed 's,:,/,g' | bc -l)")"
elif [ -n "${width}" ] && [ -z "${height}" ]; then
scale=",scale=${width}:-1:flags=lanczos"
height="$(printf %.0f "$(echo "${width} / (${ASPECTRATIO})" | sed 's,:,/,g' | bc -l)")"
elif [ -n "${width}" ] && [ -n "${height}" ]; then
scale=",scale=${width}:${height}:flags=lanczos"
else
width="${WIDTH}"
height="${HEIGHT}"
scale=
fi
fi
# add dimensions to the output file name
out="${OUT%.*}-${width}x${height}-sub.${FORMAT}"
# generate the command
cmd="ffmpeg -v warning -y -i file:${in@Q} -ss ${START}"
# -t and -to are mutually exclusive, so set duration over end stamp
# (mirrors ffmpeg default behavior)
if [[ -v LENGTH ]]; then
cmd="${cmd} -t ${LENGTH}"
elif [[ -v END ]]; then
cmd="${cmd} -to ${END}"
fi
# burn the subtitles into a short video
case "${SCODEC}" in
mov_text | subrip)
cmd="${cmd} -map 0:v:0 -c:v:0 ${VCODEC} -an -sn -vf 'subtitles=${in@Q}:si=${SIDX}' -f ${VCODEC} ${TMP}"
;;
hdmv_pgs_subtitle | dvd_subtitle)
cmd="${cmd} -an -filter_complex '[0:v:0][0:s:${SIDX}] overlay [v]' -map '[v]' -f ${VCODEC} ${TMP}"
;;
*)
show_warning "WARNING: No subtitle streams found in ${IN}. Skipping."
return
;;
esac
echo "${cmd}" >&2
eval "${cmd}"
# convert the short video into an image
case "${FORMAT}" in
gif)
cmd="ffmpeg -v warning -y -i file:${TMP} -filter_complex '[0:v] ${FPS:+fps=${FPS}${scale},}split [a][b];[a] palettegen [p];[b] fifo [c];[c][p] paletteuse' -loop 0 -f gif file:${out@Q}"
;;
webp)
cmd="ffmpeg -v warning -y -i file:${TMP} ${FPS:+-vf \'fps=${FPS}${scale}\' }-vcodec libwebp -lossless 0 -compression_level 0 -q:v 100 -loop 0 -preset picture -fps_mode passthrough -f webp file:${out@Q}"
;;
esac
echo "${cmd}" >&2
eval "${cmd}"
}
function make_image {
local in="${IN}"
local dim="${3}"
local width
local height
local out
local cmd
# compute the width and height for the image
if [[ "${dim}" = native ]]; then
width="${WIDTH}"
height="${HEIGHT}"
scale=
else
width="$(echo "${dim}" | sed -n "s,^\([0-9]*\)[:x]\([0-9]*\)$,\1,p")"
height="$(echo "${dim}" | sed -n "s,^\([0-9]*\)[:x]\([0-9]*\)$,\2,p")"
if [ -z "${width}" ] && [ -n "${height}" ]; then
scale=",scale=-1:${height}"
width="$(printf %.0f "$(echo "${height} * ${ASPECTRATIO}" | sed 's,:,/,g' | bc -l)")"
elif [ -n "${width}" ] && [ -z "${height}" ]; then
scale=",scale=${width}:-1"
height="$(printf %.0f "$(echo "${width} / (${ASPECTRATIO})" | sed 's,:,/,g' | bc -l)")"
elif [ -n "${width}" ] && [ -n "${height}" ]; then
scale=",scale=${width}:${height}"
else
width="${WIDTH}"
height="${HEIGHT}"
scale=
fi
fi
# add dimensions to the output file name
out="${OUT%.*}-${width}x${height}.${FORMAT}"
# generate the command
cmd="ffmpeg -v warning -y -ss ${START}"
# -t and -to are mutually exclusive, so set duration over end stamp (mirrors
# ffmpeg default behavior)
if [[ -v LENGTH ]]; then
cmd="${cmd} -t ${LENGTH}"
elif [[ -v END ]]; then
cmd="${cmd} -to ${END}"
fi
cmd="${cmd} -i file:${in@Q} -an -sn"
# generate separate filter strings for GIF and WebP formats
case "${FORMAT}" in
gif)
cmd="${cmd} -filter_complex '[0:v:0] ${FPS:+fps=${FPS}${scale},}split [a][b];[a] palettegen [p];[b] fifo [c];[c][p] paletteuse' -loop 0 -f gif file:${out@Q}"
;;
webp)
cmd="${cmd} -map 0:v:0 ${FPS:+-vf \'fps=${FPS}${scale}\' }-vcodec libwebp -lossless 0 -compression_level 0 -q:v 100 -loop 0 -preset picture -fps_mode passthrough -f webp file:${out@Q}"
;;
esac
echo "${cmd}" >&2
eval "${cmd}"
}
#
# Set globals
#
EMBED_SUBS=false
START=0.0
TMP=$(mktemp)
trap 'rm -f ${TMP}; exit' INT TERM ERR EXIT
#
# Parse input
#
OPTIONS=hi:o:s:e:l:d:t:p:
LONGOPTIONS=help,input:,output:,start:,end:,length:,resolution:,subtitle:,fps:
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ ${#} -ge 1 ]; do
case "${1}" in
-i | --input)
IN="${2}"
scan_input
shift 2
;;
-o | --output)
OUT="${2}"
case "${2##*.}" in
GIF | gif | Gif)
FORMAT=gif
;;
WEBP | webp | WebP)
FORMAT=webp
;;
*)
show_error "ERROR: ${2@Q} unsupported output format."
exit 3
;;
esac
shift 2
;;
-s | --start)
START="${2}"
shift 2
;;
-e | --end)
END="${2}"
shift 2
;;
-l | --length)
LENGTH="${2}"
shift 2
;;
-t | --subtitle)
EMBED_SUBS="${2}"
shift 2
;;
-d | --dimensions)
IFS=',' read -r -a DIMENSIONS <<< "${2}"
shift 2
;;
-p | --fps)
FPS="${2}"
shift 2
;;
-h | --help)
print_usage
exit
;;
--)
shift
break
;;
*)
show_error "Error"
exit 3
;;
esac
done
#
# Main
#
# Check if variables are set before running ffmpeg.
if ! [[ -v IN ]] || ! [[ -v OUT ]]; then
show_error "Try again. Usage:"
print_usage
exit 3
fi
# If duration is unset, go to the end of the video...
if ! [[ -v LENGTH ]] && ! [[ -v END ]]; then
LENGTH="$(echo "${DURATION} - ${START}" | bc -l)"
fi
# Create a GIF/WebP for all dimensions listed in the input.
for dim in "${DIMENSIONS[@]}"; do
case "${EMBED_SUBS}" in
yes | true | y)
make_image_subs "${IN}" "${OUT}" "${dim}"
;;
no | false | n)
make_image "${IN}" "${OUT}" "${dim}"
;;
both)
make_image "${IN}" "${OUT}" "${dim}"
make_image_subs "${IN}" "${OUT}" "${dim}"
;;
*)
show_error "Try again. Usage:"
print_usage
exit 3
;;
esac
done