]> git.plutz.net Git - serve0/blob - actions/watch.sh
extending cooperation functions
[serve0] / actions / watch.sh
1 #!/bin/zsh
2
3 cgi_refdata
4 info="${_GET[i]}"
5 watch="${_GET[w]:-${_COOKIE[watch]}}"
6 location="$(invalidate "${_GET[l]}" '(.*/)?\.\.(/.*)?|' /)"
7 order="${_POST[o]}"
8 search="${_POST[s]}"
9 filter="${_POST[f]}"
10 page="${_POST[pn]}"
11
12 watch_link=''
13 [ -n "$location" ] && watch_link="l=$(urlsafe "${location}")&"
14 [ -n "$info" ]     && watch_link="i=$(urlsafe "${info}")&"
15 [ -n "$order" ]    && watch_link="${watch_link}o=${order}&"
16 [ -n "$search" ]   && watch_link="${watch_link}s=${search}&"
17 [ -n "$filter" ]   && watch_link="${watch_link}f=${filter}&"
18 [ -n "$page" ]     && watch_link="${watch_link}pn=${page}"
19
20 meta="${_DATA}/meta/$info.meta"
21 head -n1 "$meta" |read length width height filename
22 [ "$length" -gt 0 ] || length=0
23 [ "$width"  -gt 0 ] || width=0
24 [ "$height" -gt 0 ] || height=0
25
26 linkinfo="$(urlsafe "$info")"
27
28 if [ "${_COOKIE[fakemp4]}" = yes ] && [ "${info##*.}" != mp4 ]; then
29   [ -e "${_DATA}/videos/${info%.*}.mp4" ] || ln -s "${_DATA}/videos/$info" "${_DATA}/mp4/${info%.*}.mp4"
30   videourl="$(urlsafe "mp4/${info%.*}.mp4")"
31 elif [ -z "$info" ]; then
32   videourl="$(urlsafe "${location#/}")"
33 else
34   videourl="$(urlsafe "videos/$info")"
35 fi
36
37 case "$watch" in
38   raspi)
39     rpiaddr="http://atoemchen.helmchyn.no-ip.biz/?action=watch&w=server&i="
40     redirect "${rpiaddr}$(urlsafe "http://jupiter:8000/${videourl}")"
41   ;;
42   480p)
43     [ $height -gt 480 ] && redirect "?action=transcode&i=${linkinfo}&t=480" \
44                         || redirect "$videourl"
45   ;;
46   240p)
47     [ $height -gt 240 ] && redirect "?action=transcode&i=${linkinfo}&t=240" \
48                         || redirect "$videourl"
49   ;;
50   download)
51     redirect "$videourl"
52   ;;
53   server)
54     [ -n "$location" ] && playuri="${treeroot%/}/${location#/}"
55     [ -n "$info" ]     && playuri="${_DATA}/videos/$info"
56
57     metafile="${_DATA}/meta/${info}.meta"
58
59     playmeta="${_DATA}/mplayer.meta"
60     fifofile="${_DATA}/mplayer.fifo"
61     statusfile="${_DATA}/mplayer.status"
62     statusfile="/tmp/mplayer.status"
63     volume="$(validate "$(cat "${_DATA}/mplayer.volume")" '[1-9]?[0-9]|100' 20)"
64     [ \! -p "$fifofile" ] && rm -f "$fifofile" && mkfifo -m 600 "$fifofile"
65
66     debug PLAYURI "$playuri"
67
68     if [ -f "$metafile" ]; then
69       cp "$metafile" "$playmeta"
70     else
71       rm "$playmeta"
72       "${_EXEC}"/helpers/genmeta.sh "$playuri" "$playmeta" >/dev/null
73     fi
74
75     export DISPLAY=":0"
76     uid="$(id |sed -rn '1s;.*uid=([0-9]+)[^0-9].*;\1;p')"
77     export XDG_RUNTIME_DIR="/run/user/$uid"  # required for Pulseaudio
78     xrandr >/dev/null  # helps enable all screens
79
80     # nohup /usr/bin/mplayer \
81     #   -slave -input file="$fifofile" \
82     #   -volume "$volume" \
83     #   "$playuri" >"$statusfile" 2>/dev/null &
84
85     # MPlayer2: (statusline on stderr)
86     nohup /usr/bin/mplayer \
87       --slave --input=file="$fifofile" \
88       --volume="$volume" \
89       "$playuri" 2>"$statusfile" >/dev/null &
90
91     chmod 600 "$statusfile"
92
93     redirect "?p=playctl&${watch_link}"
94   ;;
95   *) redirect "?p=view&${watch_link}"
96   ;;
97 esac
98
99
100