]> git.plutz.net Git - serve0/blob - pages/videoview.sh
e207c7cb343b153f597496b392789157ae9e7cc9
[serve0] / pages / videoview.sh
1 #!/bin/zsh
2
3 # Copyright 2014 Paul Hänsch
4 #
5 # This file is part of Serve0.
6
7 # Serve0 is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # Serve0 is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with Serve0.  If not, see <http://www.gnu.org/licenses/>. 
19
20 info="$_GET[\"i\"]"
21 filter="$_GET[\"f\"]"
22 search="$_GET[\"s\"]"
23 order=$(echo -E "$_GET[\"o\"]" |egrep '^(Name|Date|Length)$')
24 page=$(echo -E "$_GET[\"p\"]" |egrep '^[0-9]+$')
25 [ -z "$page" ] && page=0
26
27 page_link=''
28 [ -n "$info" ]   && page_link="i=$(echo -En "$info" |sed -r 's:%:%25:g;s:\&:%26:g;')&"
29 [ -n "$order" ]  && page_link="${page_link}o=${order}&"
30 [ -n "$search" ] && page_link="${page_link}s=${search}&"
31 [ -n "$filter" ] && page_link="${page_link}f=${filter}&"
32 page_prev="${page_link}p=$([ "$page" = 0 ] && echo 0 || echo $(($page - 1)) )"
33 page_next="${page_link}p=$(($page + 1))"
34 page_this="${page_link}p=$page"
35
36
37 taglist=$(
38   [ -r ${_DATA}/cache/taglist ] && cat ${_DATA}/cache/taglist ||\
39   for each in ${_DATA}/meta/*.meta; do
40     sed -n '2s:|:\n:gp' "$each"
41   done |sort -u |grep -xv '' |tee ${_DATA}/cache/taglist
42 )
43     
44 filterdiag() {
45   . ${_EXEC}/templates/filterdiag.html.sh
46 }
47
48 tagger() {
49   n=foot
50   hideall="$(sed -rn "s;^([^:]+):.*$;hide_inline('${n}_\\1')\\;;p" <<<"$taglist" |sort -u)hide_inline('${n}_none');"
51
52   . ${_EXEC}/templates/tagger.html.sh
53 }
54
55 view() {
56   if [ -n "$info" ]; then
57     meta="${_DATA}/meta/$info.meta"
58     video="${_DATA}/videos/$info"
59     #reenc="${_DATA}/vp8/$(sed -r 's:^(.*)\.[a-zA-Z0-9]{3,4}$:\1:' <<<"$info" )"
60     #[ -f "$reenc.mp4" ] && reenc="$reenc.mp4" || reenc="$reenc.mkv"
61     tags="$(sed -n '2p' "$meta")"
62     description="$(sed -n '3,$p' "$meta")"
63     length=0; width=0; height=0;
64     eval $(mplayer -noconsolecontrols -input file=/dev/null -slave -really-quiet -identify -frames 0 -ao null -vo null "$video" 2>/dev/null\
65     | sed -rn 's:ID_VIDEO_WIDTH=(.*):width=\1:p;s:ID_VIDEO_HEIGHT=(.*):height=\1:p;s:ID_LENGTH=(.*)(\..*)$:length=\1:p;')
66     
67     video="videos/$info"
68     . ${_EXEC}/templates/view.html.sh
69   fi
70 }
71
72 _printVideo(){
73   info="$1"
74   cache="${_DATA}/cache/$info.cache"
75   video="${_DATA}/videos/$info"
76   meta="${_DATA}/meta/$info.meta"
77   if [ "$cache" -nt "$meta" ]; then
78     cat "$cache"
79   else
80     tags="$(sed -n 2p "$meta")"
81     thumb="${_DATA}/thumbs/$info.jpg"
82     head -n1 "$meta" |read length width height filename
83     [ -z "$length" ] && length=0
84     [ -z "$width" ] && width=0
85     [ -z "$height" ] && height=0
86
87     [ -r "$meta" ] || ". ${_EXEC}/helpers/genmeta.sh" "$video" "$meta"
88     [ -r "$thumb" ] || ". ${_EXEC}/helpers/genthumb.sh" "$video" "$thumb"
89     video="$(urlsave "videos/$info")"
90     thumb="$(urlsave "thumbs/$info.jpg")"
91
92     [ "$(($length % 60))" -lt 10 ] &&\
93       minutes="$(($length / 60)):0$(($length % 60))" ||\
94       minutes="$(($length / 60)):$(($length % 60))"
95
96     linkinfo="$(urlsave "$info")"
97
98     rpiaddr="$(host pfostenpanne |sed -r 's;^.* ([0-9\.]+)$;\1;')"
99     rpibase="http://${rpiaddr}/?path=http://jupiter:8000/"
100     rpicgi="http://${rpiaddr}/?path=$(urlsave "http://jupiter:8000/?action=transcode&t=480&i=")"
101
102     [ $height -gt 480 ] && \
103        raspi="<a class=\"videolink alt raspi\" href=\"${rpicgi}${linkinfo}\">RasPi</a>" \
104     || raspi="<a class=\"videolink alt raspi\" href=\"${rpibase}${video}\">RasPi</a>"
105     [ $height -gt 480 ] && \
106       alt480="<a class=\"videolink alt 480p\" href=\"?action=transcode&i=${linkinfo}&t=480\">480p</a>" \
107     ||unset alt480
108     [ $height -gt 240 ] && \
109       alt240="<a class=\"videolink alt 240p\" href=\"?action=transcode&i=${linkinfo}&t=240\">240p</a>" \
110     ||unset alt240
111
112     tee "$cache" <<VIDEOend
113       <li class="thumb">
114         <a class="watchlink" name="${linkinfo}" href="?action=watch&i=${linkinfo}">
115           <img src="$thumb">
116         </a>
117         <a class="videolink" href="$video">$info</a>
118         ${alt480} ${alt320} ${alt240} ${raspi}<br>
119         
120         <input type="checkbox" name="tagsel" value="$info">
121         <span class="info property">${minutes}min</span>
122         <span class="info property">${width}x${height}</span>
123         $(echo -E "$tags" |sed -r 's:\|*([^|]+)\|*:<span class="info tag">\1</span>:g')
124       </div>
125 VIDEOend
126   fi
127 }
128
129 _by_name(){ find ${_DATA}/videos/ -mindepth 1 -maxdepth 1 -printf '%f\n' |sort |egrep -i "$file_pattern" }
130 _by_date(){ ls -c ${_DATA}/videos/ |egrep -i "$file_pattern" }
131 _by_length(){ find ${_DATA}/by_length/ -mindepth 1 -maxdepth 1 -printf '%f\n' |sort |sed -r 's:[0-9]{5} - ::' }
132
133 thumblist() {
134   cache="${_DATA}/cache/?o=${order}&s=${search}&f=${filter}&p=${page}"
135   if [ "$cache" -nt ${_DATA}/videos -a "$cache" -nt ${_DATA}/meta ]; then
136     cat "$cache"
137   else
138     filterex="q0"
139     for each in $(sed 's,\^, ,g' <<<"$filter"); do
140       if egrep -q '^!' <<<"$each"; then
141         this="$(sed 's,^!,,' <<<"$each")"
142         filterex="/${this}/q1;{${filterex}}"
143       else
144         filterex="/${each}/{${filterex}}"
145       fi
146     done
147     
148     case "$order" in
149       Date) genlist="_by_date"
150         ;;
151       Length) genlist="_by_length"
152         ;;
153       *) genlist="_by_name"
154         ;;
155     esac
156       
157     if [ -n "$search" ] ; then
158       #$genlist |egrep -i "$search"
159       $genlist |sed -rn 's:^(.+)$:'"${_DATA}"'/meta/\1'.meta':p' |xargs -d\\n egrep -sil "$search" |sed -rn 's:^(.*/)+([^/]+)\.meta$:\2:p'
160     elif [ "$filterex" != "q0" ]; then
161       $genlist |while read video; do
162         [ -r "${_DATA}/meta/$video.meta" ] && sed -rn "2{$filterex;q1}" "${_DATA}/meta/$video.meta" && echo -E "$video"
163       done
164     else
165       $genlist
166     fi |sed -n $(($page * $pagesize + 1)),$(($page * $pagesize + $pagesize))p |tee "$cache"
167   fi | while read line; do
168     _printVideo "$line"
169   done
170 }
171
172 case "$1" in
173   title)
174     echo "Videos"
175   ;;
176   css)
177     . ${_EXEC}/templates/videoview.css.sh
178   ;;
179   body)
180     . ${_EXEC}/templates/videoview.html.sh
181   ;;
182 esac