]> git.plutz.net Git - serve0/blob - helpers/genthumb.sh
thumbnail and meta generation
[serve0] / helpers / genthumb.sh
1 #!/bin/zsh
2 # Copyright 2014 - 2017 Paul Hänsch
3 #
4 # This file is part of Serve0
5 #
6 # Serve0 is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Serve0 is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU Affero General Public License for more details.
15 #
16 # You should have received a copy of the GNU Affero General Public License
17 # along with Serve0  If not, see <http://www.gnu.org/licenses/>.
18
19 video="$1"
20 thumb="$2"
21
22 if [ -e "$video" -a \! -e "$thumb" ]; then
23   printf '' |mplayer -input nodefault-bindings -nosound -vo null -frames 0 -identify "$video" \
24   | sort \
25   | sed -rn 's:^.*ID_LENGTH=(.*)(\..*)$:\1:p;
26              s:^.*ID_VIDEO_HEIGHT=(.*)$:\1:p;
27              s:^.*ID_VIDEO_WIDTH=(.*)$:\1:p;' \
28   | tr '\n' ' ' \
29   | read length height width 2>/dev/null
30   
31   [ "$(($height * 1000 / $width))" -gt "750" ] && width="$((75 * $width / $height))" \
32                                            || width=100
33   [ -z $width ] && width=100
34   chunk="$((${length:-0} / 5))"
35   
36   tmp="$(mktemp -d)"
37   for cnt in 1 2 3 4; do
38     printf '' |mplayer -input nodefault-bindings -nosound -vo jpeg:outdir="$tmp" -frames 1 \
39              -benchmark -vf framestep=I,scale="$width":-2 -ss "$(($cnt*$chunk))" "$video" >/dev/null 2>/dev/null
40     mv "$tmp/00000001.jpg" "${tmp}/_${cnt}.jpg"
41   done
42   montage "${tmp}"/_[1234].jpg -tile 2x2+0+0 -geometry 100x75+3+3 -background '#000000' -quality 50 "${thumb}" 2>/dev/null
43   rm -r "${tmp}"
44 fi