-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththumbnailer
executable file
·57 lines (46 loc) · 1.37 KB
/
thumbnailer
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
#!/bin/bash
die() { echo "$@"; exit 1; }
min() { [ "$1" -lt "$2" ] && echo "$1" || echo "$2"; }
extension() { basename "$1" | sed -e 's/^.*\.\([^\.]*\)$/\L\1/'; }
cache_path() { echo "${CACHE}$(realpath "$1")" | sed 's/\(.png\)\?$/.png/'; }
match_vid_ext() { extension "$1" | grep -qxE "avi|mp4|wmv|dat|3gp|ogv|mkv|mpg|mpeg|vob|fl[icv]|m2v|mov|webm|ts|mts|m4v|r[am]|qt|divx"; }
make_vid_cache() {
cached="$(cache_path "$1")"
[ -f "$cached" ] && return
mkdir -p "$(dirname "$cached")"
ffmpegthumbnailer -i "$1" -o "$cached"
}
match_img_ext() { extension "$1" | grep -qxE "bmp|jpg|jpeg|png|xpm|webp|gif"; }
make_img_cache() {
cached="$(cache_path "$1")"
[ -f "$cached" ] && return
mkdir -p "$(dirname "$cached")"
convert "$1[0]" -resize "${MAX_W}x${MAX_H}>" "$cached"
}
gen_thumb() {
if match_vid_ext "$1"; then
make_vid_cache "$1"
elif match_img_ext "$1"; then
make_img_cache "$1"
fi
}
for binary in ffmpegthumbnailer convert; do
which "$binary" &>/dev/null || die "Required '$binary' is not available."
done
CACHE="$HOME/.cache/thumbs"
MAX_W=400
MAX_H=400
if ! ( match_vid_ext "$1" || match_img_ext "$1" ); then
die "Extension not supported"
fi
while [ "${1:0:1}" = "-" ]; do
case "${1:1}" in
path) cache_path "$2"; exit 0 ;;
*) die "Option not supported" ;;
esac
shift
done
while [ -n "$1" ]; do
gen_thumb "$1" && cache_path "$1"
shift
done