From: Paul Hänsch Date: Sat, 7 Jul 2018 16:12:43 +0000 (+0200) Subject: thumbnail and meta generation X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=b3b0d44a61029470e449827a1fa4b686e4279b7c;p=serve0 thumbnail and meta generation --- diff --git a/index.cgi b/index.cgi index 000e855..68d6b5b 100755 --- a/index.cgi +++ b/index.cgi @@ -3,6 +3,8 @@ [ -z "${_EXEC%/}" ] && _EXEC="$(realpath "${0%/*}")" || _EXEC="${_EXEC%/}" [ -z "${_DATA%/}" ] && _DATA=. || _DATA="${_DATA%/}" +file_pattern='^.*\.(mov|ts|mpg|mpeg|mp4|m4v|avi|mkv|flv|sfv|wmv|ogm|ogv|webm|iso|rmvb)$' + . "$_EXEC/cgilite/cgilite.sh" . "$_EXEC/widgets.sh" @@ -30,7 +32,12 @@ elif [ -f "$_DATA/$ITEM" ]; then tag) ;; thumbnail) - printf 'Status: 404 Not Found\r\nContent-Length 0:\r\n\r\n' + . "$_EXEC/cgilite/file.sh" + index="$_DATA/${ITEM%/*}/.index" + thumb="$index/${ITEM##*/}.jpg" + [ -d "$index" -a \! -f "$thumb" ] \ + && file="$_DATA/$ITEM" . "$_EXEC/thumbnail.sh" + FILE "$thumb" ;; delete) ;; diff --git a/list.sh b/list.sh index 61099e7..9abf0e9 100644 --- a/list.sh +++ b/list.sh @@ -3,10 +3,23 @@ list_item(){ name="$(HTML "$1")" path="$(HTML "$ITEM/$1")" + meta="$_DATA/$ITEM/.index/meta" if [ -d "$_DATA/$ITEM/$1" ]; then printf '[a .list .dir href="%s" %s]' \ "$path" "$name" + elif [ -f "$meta" ]; then + read -r length width height tags comment n <<-EOF + $(grep -m1 -F " $1" "$meta") + EOF + printf '[div .list .file + [a href="%s" [img src="%s?a=thumbnail"] %s] + [span .time %i:%imin] [span .dim %ix%i] %s + ]' \ + "$path" "$path" "$name" \ + "$((length / 60))" "$((length % 60))" \ + "$width" "$height" \ + "$(printf %s\\n "${tags#tags=}" |tr , ' ' |xargs printf '[span .tag %s]')" else printf '[div .list .file [a href="%s" [img src="%s?a=thumbnail"] %s]]' \ "$path" "$path" "$name" @@ -18,8 +31,8 @@ list_items(){ [ "$mode" = browse -a "$ITEM" ] && printf '..\n' if [ "$mode" = browse ]; then - [ -d "$_DATA/$ITEM/.index" -a "$_DATA/$ITEM" -nt "$_DATA/$ITEM/.index/meta" ] \ - && . "$_EXEC/update_meta.sh" + [ -d "$_DATA/$ITEM/.index" -a \! "$_DATA/$ITEM" -ot "$_DATA/$ITEM/.index/meta" ] \ + && dir="$_DATA/$ITEM" . "$_EXEC/update_meta.sh" (cd "$_DATA/$ITEM"; find ./ -type d \! -name .index -mindepth 1 -maxdepth 1 \ -exec stat -c '%Y %n' '{}' + @@ -27,6 +40,11 @@ list_items(){ -exec stat -c '%Y %n' '{}' + ) elif [ "$mode" = index ]; then + find "$_DATA/$ITEM" -type d -name .index \ + | while d="$(line)"; do + [ ! "${d%/.index}" -ot "$d/meta" ] \ + && dir="${d%/.index}" . "$_EXEC/update_meta.sh" + done (cd "$_DATA/$ITEM"; find ./ -type f \ -exec stat -c '%Y %n' '{}' + diff --git a/thumbnail.sh b/thumbnail.sh new file mode 100644 index 0000000..bf5964a --- /dev/null +++ b/thumbnail.sh @@ -0,0 +1,44 @@ +#!/bin/sh + +# Use: $index $thumb $file_pattern + +if [ \! -e "$thumb" ] && { printf %s "$file" |grep -qE "$file_pattern"; }; then + read l h w <<__EOF + $(printf '' \ + | mplayer -input nodefault-bindings -nosound -vo null -identify -frames 0 "$file" 2>&- \ + | sort | sed -rn ' + s:ID_LENGTH=(.*)(\..*)$:\1:p; + s:ID_VIDEO_HEIGHT=(.*):\1:p; + s:ID_VIDEO_WIDTH=(.*):\1:p;' \ + | tr '\n' ' ' + ) +__EOF + + [ "$((h * 1000 / w))" -gt "750" ] \ + && w="$((75 * w / h))" || w=100 + chunk="$((${l:-10} / 5))" + + tmp="$(mktemp -d)" + for cnt in 1 2 3 4; do + printf '' \ + | mplayer -input nodefault-bindings -nosound -benchmark \ + -noconfig all -really-quiet \ + -frames 1 -ss "$((cnt * chunk))" \ + -vf "framestep=I,scale=${w}:-2" \ + -vo "jpeg:outdir=${tmp}" \ + "$file" 2>&- + mv "${tmp}/00000001.jpg" "${tmp}/_${cnt}.jpg" + done + printf '' \ + | mplayer -input nodefault-bindings -nosound -benchmark \ + -noconfig all -really-quiet \ + -vf scale=127:-2,tile=2:2:4:0:2 \ + -vo "jpeg:quality=60:outdir=${tmp}"\ + "mf://$tmp/_*.jpg" 2>&- + + mv "${tmp}/00000001.jpg" "$thumb" + rm -r "${tmp}" + +elif [ \! -e "$thumb" ]; then + touch "$thumb" +fi diff --git a/update_meta.sh b/update_meta.sh new file mode 100644 index 0000000..04fc77e --- /dev/null +++ b/update_meta.sh @@ -0,0 +1,39 @@ +#!/bin/sh + +file_pattern='^.*\.(mov|ts|mpg|mpeg|mp4|m4v|avi|mkv|flv|sfv|wmv|ogm|ogv|webm|iso|rmvb)$' +meta="${dir}/.index/meta" + +meta_line() { + video="$1" + + read l h w <<__EOF + $(printf '' \ + | mplayer -input nodefault-bindings -nosound -vo null -identify -frames 0 "$video" 2>&- \ + | sort | sed -rn ' + s:ID_LENGTH=(.*)(\..*)$:\1:p; + s:ID_VIDEO_HEIGHT=(.*):\1:p; + s:ID_VIDEO_WIDTH=(.*):\1:p;' \ + | tr '\n' ' ' + ) +__EOF + printf '%i\t%i\t%i\ttags=\tcomment=\t%s\n' \ + "$l" "$w" "$h" "${video##*/}" +} + + +if [ \! -f "$meta" ]; then + find "$dir" -type f -mindepth 1 -maxdepth 1 \ + | grep -E "$file_pattern" \ + | while v="$(line)"; do + meta_line "$v" + done >"$meta" +else + find "$dir" -type f -mindepth 1 -maxdepth 1 \ + | grep -E "$file_pattern" \ + | while v="$(line)"; do + if [ "$v" -nt "$meta" ]; then + grep -qF " ${v}" "$meta" \ + || meta_line "$v" + fi + done >>"$meta" +fi