]> Git — Sourcephile - julm/camera.git/blob - index.sh
css: improve header
[julm/camera.git] / index.sh
1 #!/usr/bin/env bash
2 # Copyright: Julien Moutinho <julm+camera@sourcephile.fr>
3 # License: AGPL-3.0-or-later
4 # Usage:
5 # $ mv julm/2023/src/02/IMG_20230228_111645{,.fav}.jpg
6 # $ echo >julm/2023/src/02/IMG_20230228_111645.fav.txt "Some HTML <b>comment</b> about the picture"
7 # $ ./index.sh julm/2023/src/02
8 # $ sensible-browser julm/2023/fav/02/index.html
9 #
10 # NginxConfig:
11 # locations."/julm/perso/camera/" = {
12 # alias = "${root}/julm/perso/camera/";
13 # basicAuthFile = "/run/credentials/nginx.service/autogeree.net.www.julm.perso.camera.htpasswd";
14 # };
15 # # Disable basicAuthFile for by-uuid
16 # locations."~ ^/julm/perso/camera/([0-9]+/[0-9][0-9]/by-uuid/[0-9a-f-]+/.+)$" = {
17 # alias = "${root}/julm/perso/camera/$1";
18 # };
19
20 # shellcheck disable=SC2086
21
22 cd "${0%/*}"
23 set -eu
24 shopt -s nullglob
25
26 ffmpeg_ () { local -; set -x; nice -19 ffmpeg </dev/null -hide_banner -loglevel warning -stats -y "$@"; }
27 magick_ () { local -; set -x; nice -19 magick "$@"; }
28 mv_ () { local -; mv -i >&2 "$@"; }
29
30 for dir in "$@"; do
31 IFS=/ read -r user year x month x <<<"$dir"
32 test -n "$year" || exec "$0" "$user"/2*/src/*/
33 test -n "$month" || exec "$0" "$user"/"$year"/src/*/
34 pushd "$user/$year" >/dev/null
35 test -d src || { popd; continue; }
36 mkdir -p fav
37 # cp, not ln, because in push.sh --copy-links cannot be used for by-uuid/
38 cp -f --remove-destination -t fav ../../index.css
39 test -d src/"$month" || { popd; continue; }
40 mkdir -p fav/"$month"
41 mkdir -p {tmp,wip}/fav/"$month"
42 {
43 genDate=$(date +%s)
44 cat <<EOF
45 <!doctype html>
46 <!-- Generator: https://git.sourcephile.fr/julm/camera.git -->
47 <!-- GenerationDate: $(date -R -d@"$genDate") -->
48 <html>
49
50 <head lang="fr">
51 <meta charset="utf-8">
52 <title>camera/$user/$year/$month</title>
53 <link rel="stylesheet" href="../index.css?$genDate">
54 </head>
55
56 <body>
57
58 <nav class="path">
59 <a href='../../..'>camera</a>/ <a href='../..'>$user</a>/ <a href='..'>$year</a>/
60 EOF
61 for m in {01..12}; do
62 if [ "$m" -eq 01 ]; then printf '{ '; fi
63 if [ "$m" -eq "$month" ]; then
64 printf %s "$month"
65 else
66 printf %s "<a href='../$m'>$m</a>"
67 fi
68 if [ "$m" -eq 12 ]; then printf ' }'; else printf ', '; fi
69 done
70 cat <<EOF
71 </nav>
72
73 <div class="camera">
74 <ul>
75 EOF
76 echo >&2 "$dir: processing favorites in chronological order"
77 # Time is encoded in filenames like so: MM/{IMG,VID}_YYYYMMDD_hhmmss.*
78 # hence sorting after the underscore (2nd field, 5th char).
79 (cd src; find -L "$month" -name '*.fav.*' -not -name "*.txt") | sort -t/ -k2.5 |
80 while read -r src; do
81 echo >&2 "$dir: processing $year/src/$src"
82 base=${src%.*}
83 name=${base#*/}
84 id=${name%%.*}
85 creationDate=$(printf %s "${src##*/???_}" | sed -e 's/\(....\)\(..\)\(..\)_\(..\)\(..\)\(..\).*/\1-\2-\3 \4:\5:\6/')
86 echo "<li id='$id'><div class='item'>"
87 test -e fav/"$base".uuid ||
88 uuidgen --random >fav/"$base".uuid
89 uuid=$(cat fav/"$base".uuid)
90 mkdir -p fav/"$month"/by-uuid/"$uuid"
91 case "$src" in
92 *.jpg)
93 echo "<picture>"
94 printf "<source type='image/avif' srcset='"
95 for w in {1200,600,300}; do
96 test fav/"$base"."$w"x.avif -nt src/"$src" || {
97 magick_ src/"$src" \
98 -sampling-factor 4:2:0 -colorspace sRGB \
99 -auto-orient -thumbnail "$w"x -unsharp 0x.5 \
100 wip/fav/"$base"."$w"x.avif
101 mv_ {wip/,}fav/"$base"."$w"x.avif
102 }
103 printf %s "$name.${w}x.avif ${w}w, "
104 done
105 echo "' />"
106 w=600
107 test fav/"$base"."$w"x.avif -nt src/"$src" || {
108 magick_ -define jpeg:size=$((w * 2))x src/"$src" \
109 -sampling-factor 4:2:0 -interlace JPEG -colorspace sRGB \
110 -auto-orient -thumbnail "$w"x -unsharp 0x.5 \
111 wip/fav/"$base"."$w"x.jpg
112 mv_ {wip/,}fav/"$base"."$w"x.avif
113 }
114 echo "<source type='image/jpeg' srcset='$name.${w}x.jpg ${w}w' />"
115 echo "<img src='$name.${w}x.jpg' alt='$creationDate' />"
116 echo "</picture>"
117 dst="$name".1200x.avif
118 ln -fs -t fav/"$month"/by-uuid/"$uuid" ../../"$dst"
119 ;;
120 *.mp4)
121 need_scale () {
122 orient=$(ffprobe -v 0 -select_streams v:0 -show_entries stream_side_data=rotation -of default=nw=1:nk=1 src/"$src")
123 if [ -z "$orient" ]; then scale="-2:360"; else scale="360:-2"; fi
124 }
125 test fav/"$base".avif -nt src/"$src" || {
126 need_scale
127 ffmpeg_ -i src/"$src" \
128 -map 0:v \
129 -filter:v format=yuv420p10le,scale="$scale" \
130 -frames:v 1 -crf 35 \
131 wip/fav/"$base".avif
132 mv_ {wip/,}fav/"$base".avif
133 }
134 need_opus () {
135 test tmp/fav/"$base".opus -nt src/"$src" || {
136 ffmpeg_ -i src/"$src" \
137 -map 0:a -c:a libopus -b:a 64k -application voip \
138 wip/fav/"$base".opus
139 mv_ {wip,tmp}/fav/"$base".opus
140 }
141 }
142 test fav/"$base".360p.av1.webm -nt src/"$src" || {
143 need_scale
144 need_opus
145 # See https://gitlab.com/AOMediaCodec/SVT-AV1/-/blob/master/Docs/CommonQuestions.md
146 # See https://gist.github.com/BlueSwordM/86dfcb6ab38a93a524472a0cbe4c4100
147 # -preset Range from 0 to 13, with higher numbers providing a higher encoding speed;
148 # -crf Range is 0-63, with the default being 50.
149 # Lower values correspond to higher quality and greater file size
150 # yuv420p
151 # yuv420p10le can represent more shades of grey and colors
152 # and is less prone to certain artifacts, such as color
153 # banding and loss of detail in low luma areas
154 # cost in terms of resulting file size (~5%),
155 # can also be more compute-intensive than 8-bit in some decoders
156 # tune=0 subjective mode often results in an image with greater
157 # sharpness and is intended to produce a result that appears to humans
158 # to be of high quality (as opposed to doing well on basic objective measures,
159 # such as PSNR)
160 # scd=1 biases the encoders' scene detection to insert more intra refreshes if needed,
161 # and reduce temporal dependencies around scene-changes.
162 # scm=0 screen content mode decision making. 0 is best for live-action.
163 ffmpeg_ -i src/"$src" -i tmp/fav/"$base".opus \
164 -map 0:v \
165 -filter:v format=yuv420p,scale="$scale" \
166 -c:v libsvtav1 -preset 10 -crf 35 \
167 -svtav1-params keyint=30s:tune=0:enable-overlays=1:scd=1:scm=0:fast-decode=1:tile-columns=2 \
168 -map 1:a -c:a copy \
169 wip/fav/"$base".360p.av1.webm
170 mv_ {wip/,}fav/"$base".360p.av1.webm
171 }
172 dst="$name".360p.av1.webm
173 ln -fs -t fav/"$month"/by-uuid/"$uuid" ../../"$dst"
174 #test fav/"$base".360p.vp9.webm -nt src/"$src" || {
175 # need_scale
176 # need_opus
177 # # See https://developers.google.com/media/vp9/settings/vod#recommended_settings
178 # # See https://gist.github.com/mrintrepide/3033c35ee9557e66cff7806f48dbd339
179 # set -- \
180 # -filter:v format=yuv420p10le,scale="$scale" \
181 # -c:v libvpx-vp9 -quality good -crf 20 \
182 # -minrate 138k -b:v 276k -maxrate 400k \
183 # -cpu-used 4 -static-thresh 0 -tile-columns 0 -tile-rows 0 -frame-parallel 0 \
184 # -row-mt 1 -aq-mode 0 -auto-alt-ref 6 -lag-in-frames 25 -enable-tpl 1
185 # test -e tmp/fav/"$base".360p.vp9.webm-0.log || {
186 # # FIXME: how to disable h264 debug log?
187 # ffmpeg 2>/dev/null -i src/"$src" \
188 # -map 0:v "$@" -pass 1 -passlogfile wip/fav/"$base".360p.vp9.webm \
189 # -f null \
190 # /dev/null
191 # mv_ -t tmp/fav/"${base%/*}" wip/fav/"$base".360p.vp9.webm-*.log
192 # }
193 # ffmpeg -i src/"$src" -i tmp/fav/"$base".opus \
194 # -map 0:v "$@" -pass 2 -passlogfile tmp/fav/"$base".360p.vp9.webm \
195 # -map 1:a -c:a copy \
196 # wip/fav/"$base".360p.vp9.webm
197 # mkdir -p fav/"$month"/by-uuid/"$uuid"
198 # mv_ wip/fav/"$base".360p.vp9.webm fav/"$base".360p.vp9.webm
199 # rm -fv tmp/fav/"$base".360p.vp9.webm-*.log
200 #}
201 #test fav/"$base".mp4 -nt src/"$src" || {
202 # ffmpeg -i src/"$src" \
203 # -filter:v format=yuv420p \
204 # -c:v libx264 -preset medium -crf 22 \
205 # -tune zerolatency \
206 # -maxrate 1M -bufsize 2M \
207 # -movflags use_metadata_tags +faststart \
208 # -c:a libopus -base:a 64k -application voip \
209 # -filter:v "scale=iw/2:ih/2" \
210 # wip/fav/"$base".mp4
211 # mv_ wip/fav/"$base".mp4 fav/"$base".mp4
212 #}
213 # class='orient$orient'
214 echo "<video controls preload=none poster='$name.avif'>"
215 # See https://jakearchibald.com/2022/html-codecs-parameter-for-av1/
216 # and ffmpeg -i fav/$month/foo.av1.webm -c:v copy -bsf:v trace_headers -f null /dev/null |&
217 # grep -e seq_profile -e seq_level_idx -e seq_tier -e high_bitdepth -e twelve_bit
218 P=0; LL=01; T=M; DD=08
219 echo "<source type='video/webm; codecs=av01.$P.$LL$T.$DD' src='$name.360p.av1.webm' />"
220 echo "<p><a href='$name.360p.av1.webm'>$name.360p.av1.webm</a></p>"
221 echo "</video>"
222 esac
223 touch -a src/"$base".txt
224 printf %s "<span class='comment'>"
225 sed -e '$q;s/$/<br \/>\n/' src/"$base".txt
226 echo "</span>"
227 echo "</div>"
228 echo "<span class='infos'>"
229 echo " <span class='creationDate'>$creationDate</span>"
230 echo "</span>"
231 echo "<span class='links'>"
232 echo " <a class='by-uuid' href='by-uuid/$uuid/${dst##*/}' title='Lien public vers cette capture' target='_blank'>@</a>"
233 echo " <a class='anchor' href='#$id' title='Ancre vers cette capture'>#</a>"
234 echo "</span>"
235 echo "</li>"
236 echo
237 done
238 cat <<EOF
239 </ul>
240 </div>
241
242 </body>
243 </html>
244 EOF
245 } >tmp/fav/"$month"/index.html
246 mv_ -f {tmp/,}fav/"$month"/index.html
247
248 echo >&2 "$dir: removing any deleted favorites"
249 (cd fav; find -L "$month" -mindepth 1 -not -path '*/by-uuid/*' -type f -not -name index.html) |
250 while read -r fav; do
251 base=${fav%.fav.*}
252 name=${base#*/}
253 unset hasSrc
254 for src in src/"$base".fav.*; do hasSrc=set; done
255 test "${hasSrc:+set}" || {
256 echo >&2 "$dir: removing $year/fav/$fav"
257 rm -f fav/"$fav"
258 rm -fv fav/"$month"/by-uuid/*/"${name%.*}".*
259 }
260 done
261 rmdir -p 2>/dev/null fav/"$month"/by-uuid/*/ || true
262 rmdir -p 2>/dev/null wip/fav/* || true
263 popd >/dev/null
264 done