]> Git — Sourcephile - julm/camera.git/blob - index.sh
index: fix resolution detection
[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 time 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 unset creationDoMOld
41 unset creationDateOld
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: {IMG,VID}_YYYYMMDD_hhmmss.*
78 # hence sorting after the underscore (1st field, 5th char).
79 # To support subdirectories, sorting is done on the prefix %f
80 # which is replaced by %p thereafter.
81 (cd src; find -L "$month" -name '*.fav.*' -not -name "*.txt" -printf '%f %p\n') | sort -t/ -k1.5 | cut -f2 -d ' ' |
82 while read -r src; do
83 echo >&2 "$dir: processing $year/src/$src"
84 base=${src%.*}
85 name=${base#*/}
86 id=${name%%.*}
87 creationDate=$(printf %s "${src##*/???_}" | sed -e 's/\(....\)\(..\)\(..\)_\(..\)\(..\)\(..\).*/\1-\2-\3 \4:\5:\6/')
88 creationDoM=$(date +'%A %_d %B' -d "$creationDate")
89 if test "$creationDoM" != "${creationDoMOld-}"; then
90 dom=$(date +'%d' -d "$creationDate")
91 echo "<li id='$dom' class='day'><div>"
92 echo "<a class='day-current' href='#$dom'>$creationDoM</a>"
93 echo "<div>"
94 test ! "${creationDateOld:+set}" ||
95 echo "<a class='day-previous' href='#$(date +'%d' -d "$creationDateOld")'>^</a>"
96 echo "</div>"
97 echo "</div></li>"
98 creationDoMOld=$creationDoM
99 creationDateOld=$creationDate
100 fi
101 test -e fav/"$base".uuid ||
102 uuidgen --random >fav/"$base".uuid
103 uuid=$(cat fav/"$base".uuid)
104 mkdir -p fav/"${base%/*}"
105 mkdir -p {tmp,wip}/fav/"${base%/*}"
106 mkdir -p fav/"$month"/by-uuid/"$uuid"
107 wow=
108 test "${src##*.wow.}" = "${src}" || wow=wow
109 need_orient () {
110 # Slow, but needed to make wide videos span two columns of the CSS grid
111 IFS=' ' read -r width height rotation <<<"$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height:stream_side_data=rotation -of csv=p=0:s='\ ' src/"$src")"
112 case "$rotation" in
113 (90|-90) w=$width; width=$height; height=$w;;
114 esac
115 if [ "$width" -gt "$height" ]
116 then scale="-2:360"; orient="landscape"
117 else scale="360:-2"; orient="portrait"
118 fi
119 }
120 case "$src" in
121 *.jpg)
122 echo "<li id='$id' class='$wow'><div class='item'>"
123 echo "<picture>"
124 printf "<source type='image/avif' srcset='"
125 for w in {1200,600,300}; do
126 test fav/"$base"."$w"x.avif -nt src/"$src" || {
127 magick_ src/"$src" \
128 -sampling-factor 4:2:0 -colorspace sRGB \
129 -auto-orient -thumbnail "$w"x -unsharp 0x.5 \
130 wip/fav/"$base"."$w"x.avif
131 mv_ {wip/,}fav/"$base"."$w"x.avif
132 }
133 printf %s "$name.${w}x.avif ${w}w, "
134 done
135 echo "' />"
136 w=600
137 test fav/"$base"."$w"x.jpg -nt src/"$src" || {
138 magick_ -define jpeg:size=$((w * 2))x src/"$src" \
139 -sampling-factor 4:2:0 -interlace JPEG -colorspace sRGB \
140 -auto-orient -thumbnail "$w"x -unsharp 0x.5 \
141 wip/fav/"$base"."$w"x.jpg
142 mv_ {wip/,}fav/"$base"."$w"x.jpg
143 }
144 echo "<source type='image/jpeg' srcset='$name.${w}x.jpg ${w}w' />"
145 echo "<img src='$name.${w}x.jpg' alt='$creationDate' />"
146 echo "</picture>"
147 dst="$name".1200x.avif
148 ln -fs -t fav/"$month"/by-uuid/"$uuid" ../../"$dst"
149 ;;
150 *.mp4)
151 need_orient
152 echo "<li id='$id' class='$wow vid-orient-$orient'><div class='item'>"
153 test fav/"$base".avif -nt src/"$src" || {
154 ffmpeg_ -i src/"$src" \
155 -map 0:v \
156 -filter:v format=yuv420p10le,scale="$scale" \
157 -frames:v 1 -crf 33 \
158 wip/fav/"$base".avif
159 mv_ {wip/,}fav/"$base".avif
160 }
161 need_opus () {
162 test tmp/fav/"$base".opus -nt src/"$src" || {
163 ffmpeg_ -i src/"$src" \
164 -map 0:a -c:a libopus -b:a 64k -application voip \
165 wip/fav/"$base".opus
166 mv_ {wip,tmp}/fav/"$base".opus
167 }
168 }
169 test fav/"$base".360p.av1.webm -nt src/"$src" || {
170 need_opus
171 ffmpeg_ -i src/"$src" -i tmp/fav/"$base".opus \
172 -map 0:v \
173 -filter:v format=yuv420p,scale="$scale" \
174 -c:v libsvtav1 -preset 10 -crf 33 \
175 -svtav1-params input-depth=8:keyint=10s:tune=0:enable-overlays=1:scd=1:scm=0:fast-decode=1:film-grain=10 \
176 -map 1:a -c:a copy \
177 wip/fav/"$base".360p.av1.webm
178 mv_ {wip/,}fav/"$base".360p.av1.webm
179 }
180 dst="$name".360p.av1.webm
181 ln -fs -t fav/"$month"/by-uuid/"$uuid" ../../"$dst"
182 #test fav/"$base".360p.vp9.webm -nt src/"$src" || {
183 # need_opus
184 # # See https://developers.google.com/media/vp9/settings/vod#recommended_settings
185 # # See https://gist.github.com/mrintrepide/3033c35ee9557e66cff7806f48dbd339
186 # set -- \
187 # -filter:v format=yuv420p10le,scale="$scale" \
188 # -c:v libvpx-vp9 -quality good -crf 20 \
189 # -minrate 138k -b:v 276k -maxrate 400k \
190 # -cpu-used 4 -static-thresh 0 -tile-columns 0 -tile-rows 0 -frame-parallel 0 \
191 # -row-mt 1 -aq-mode 0 -auto-alt-ref 6 -lag-in-frames 25 -enable-tpl 1
192 # test -e tmp/fav/"$base".360p.vp9.webm-0.log || {
193 # # FIXME: how to disable h264 debug log?
194 # ffmpeg 2>/dev/null -i src/"$src" \
195 # -map 0:v "$@" -pass 1 -passlogfile wip/fav/"$base".360p.vp9.webm \
196 # -f null \
197 # /dev/null
198 # mv_ -t tmp/fav/"${base%/*}" wip/fav/"$base".360p.vp9.webm-*.log
199 # }
200 # ffmpeg -i src/"$src" -i tmp/fav/"$base".opus \
201 # -map 0:v "$@" -pass 2 -passlogfile tmp/fav/"$base".360p.vp9.webm \
202 # -map 1:a -c:a copy \
203 # wip/fav/"$base".360p.vp9.webm
204 # mkdir -p fav/"$month"/by-uuid/"$uuid"
205 # mv_ wip/fav/"$base".360p.vp9.webm fav/"$base".360p.vp9.webm
206 # rm -fv tmp/fav/"$base".360p.vp9.webm-*.log
207 #}
208 #test fav/"$base".mp4 -nt src/"$src" || {
209 # ffmpeg -i src/"$src" \
210 # -filter:v format=yuv420p \
211 # -c:v libx264 -preset medium -crf 22 \
212 # -tune zerolatency \
213 # -maxrate 1M -bufsize 2M \
214 # -movflags use_metadata_tags +faststart \
215 # -c:a libopus -base:a 64k -application voip \
216 # -filter:v "scale=iw/2:ih/2" \
217 # wip/fav/"$base".mp4
218 # mv_ wip/fav/"$base".mp4 fav/"$base".mp4
219 #}
220 # class='orient$orient'
221 echo "<video controls preload=none poster='$name.avif'>"
222 # See https://jakearchibald.com/2022/html-codecs-parameter-for-av1/
223 # and ffmpeg -i fav/$month/foo.av1.webm -c:v copy -bsf:v trace_headers -f null /dev/null |&
224 # grep -e seq_profile -e seq_level_idx -e seq_tier -e high_bitdepth -e twelve_bit
225 P=0; LL=01; T=M; DD=08
226 echo "<source type='video/webm; codecs=av01.$P.$LL$T.$DD' src='$name.360p.av1.webm' />"
227 echo "<p><a href='$name.360p.av1.webm'>$name.360p.av1.webm</a></p>"
228 echo "</video>"
229 ;;
230 *.av1.webm)
231 need_orient
232 echo "<li id='$id' class='$wow vid-orient-$orient'><div class='item'>"
233 test fav/"$base".avif -nt src/"$src" || {
234 ffmpeg_ -i src/"$src" \
235 -map 0:v \
236 -filter:v format=yuv420p10le,scale="$scale" \
237 -frames:v 1 -crf 33 \
238 wip/fav/"$base".avif
239 mv_ {wip/,}fav/"$base".avif
240 }
241 test fav/"$base".webm -nt src/"$src" || {
242 ln -f {src,fav}/"$base".webm
243 }
244 dst="$name".webm
245 ln -fs -t fav/"$month"/by-uuid/"$uuid" ../../"$dst"
246 echo "<video controls preload=none poster='$name.avif'>"
247 # See https://jakearchibald.com/2022/html-codecs-parameter-for-av1/
248 # and ffmpeg -i fav/$month/foo.av1.webm -c:v copy -bsf:v trace_headers -f null /dev/null |&
249 # grep -e seq_profile -e seq_level_idx -e seq_tier -e high_bitdepth -e twelve_bit
250 P=0; LL=01; T=M; DD=08
251 echo "<source type='video/webm; codecs=av01.$P.$LL$T.$DD' src='$name.webm' />"
252 echo "<p><a href='$name.webm'>$name.webm</a></p>"
253 echo "</video>"
254 esac
255 touch -a src/"$base".txt
256 printf %s "<span class='comment'>"
257 sed -e '$q;s/$/<br \/>\n/' src/"$base".txt
258 echo "</span>"
259 echo "</div>" # class='item'
260 echo "<span class='infos'>"
261 echo " <span class='creationDate'>$creationDate</span>"
262 echo "</span>"
263 echo "<span class='links'>"
264 echo " <a class='by-uuid' href='https://autogeree.net/julm/perso/camera/$year/$month/by-uuid/$uuid/${dst##*/}' title='Lien public vers cette capture' target='_blank'>@</a>"
265 echo " <a class='anchor' href='#$id' title='Ancre vers cette capture'>#</a>"
266 echo "</span>"
267 echo "<span class='downloads'>"
268 echo " <a class='download' href='${dst##*/}' title='Enregistrer cette capture'>Enregistrer</a>"
269 echo "</span>"
270 echo "</li>"
271 echo
272 done
273 cat <<EOF
274 </ul>
275 </div>
276
277 </body>
278 </html>
279 EOF
280 } >tmp/fav/"$month"/index.html
281 mv_ -f {tmp/,}fav/"$month"/index.html
282
283 echo >&2 "$dir: removing any deleted favorites"
284 (cd fav; find -L "$month" -mindepth 1 -not -path '*/by-uuid/*' -type f -not -name index.html) |
285 while read -r fav; do
286 base=$fav
287 base=${base%.300x.avif}
288 base=${base%.600x.avif}
289 base=${base%.600x.jpg}
290 base=${base%.1200x.avif}
291 base=${base%.avif}
292 base=${base%.360p.av1.webm}
293 base=${base%.webm}
294 base=${base%.uuid}
295 name=${base#*/}
296 unset hasSrc
297 for src in src/"$base".???; do hasSrc=set; done
298 test "${hasSrc:+set}" || {
299 echo >&2 "$dir: removing $year/fav/$fav"
300 rm -f fav/"$fav"
301 rm -f fav/"$month"/by-uuid/*/"$name".{1200x.avif,avif,360p.av1.webm,webm,uuid}
302 }
303 done
304 rmdir -p 2>/dev/null fav/"$month"/by-uuid/*/ || true
305 rmdir -p 2>/dev/null wip/fav/* || true
306 popd >/dev/null
307 done