]> Git — Sourcephile - julm/camera.git/blob - index.sh
index: infer years
[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 <ul>
60 <li><a href='../../..'>camera</a>/ <a href='../..'>$user</a>/ <a href='..'>$year</a>/ $month</li>
61 </ul></nav>
62
63 <div class="camera">
64 <ul>
65 EOF
66 echo >&2 "$dir: processing favorites in chronological order"
67 # Time is encoded in filenames like so: MM/{IMG,VID}_YYYYMMDD_hhmmss.*
68 # hence sorting after the underscore (2nd field, 5th char).
69 (cd src; find "$month" -name '*.fav.*' -not -name "*.txt") | sort -n -t/ -k2.5 |
70 while read -r src; do
71 echo >&2 "$dir: processing $year/src/$src"
72 base=${src%.*}
73 name=${base##*/}
74 id=${name%%.*}
75 creationDate=$(printf %s "${src##*/???_}" | sed -e 's/\(....\)\(..\)\(..\)_\(..\)\(..\)\(..\).*/\1-\2-\3 \4:\5:\6/')
76 echo "<li id='$id'><div class='item'>"
77 test -e fav/"$base".uuid ||
78 uuidgen --random >fav/"$base".uuid
79 uuid=$(cat fav/"$base".uuid)
80 mkdir -p fav/"$month"/by-uuid/"$uuid"
81 case "$src" in
82 *.jpg)
83 echo "<picture>"
84 printf "<source type='image/avif' srcset='"
85 for w in {1200,600,300}; do
86 test fav/"$base"."$w"x.avif -nt src/"$src" || {
87 magick_ src/"$src" \
88 -sampling-factor 4:2:0 -colorspace sRGB \
89 -auto-orient -thumbnail "$w"x -unsharp 0x.5 \
90 wip/fav/"$base"."$w"x.avif
91 mv_ {wip/,}fav/"$base"."$w"x.avif
92 }
93 printf %s "$name.${w}x.avif ${w}w, "
94 done
95 echo "' />"
96 w=600
97 test fav/"$base"."$w"x.avif -nt src/"$src" || {
98 magick_ -define jpeg:size=$((w * 2))x src/"$src" \
99 -sampling-factor 4:2:0 -interlace JPEG -colorspace sRGB \
100 -auto-orient -thumbnail "$w"x -unsharp 0x.5 \
101 wip/fav/"$base"."$w"x.jpg
102 mv_ {wip/,}fav/"$base"."$w"x.avif
103 }
104 echo "<source type='image/jpeg' srcset='$name.${w}x.jpg ${w}w' />"
105 echo "<img src='$name.${w}x.jpg' alt='$creationDate' />"
106 echo "</picture>"
107 dst="$name".1200x.avif
108 ln -fs -t fav/"$month"/by-uuid/"$uuid" ../../"$dst"
109 ;;
110 *.mp4)
111 test fav/"$base".avif -nt src/"$src" || {
112 ffmpeg_ -i src/"$src" \
113 -map 0:v \
114 -filter:v format=yuv420p,scale='trunc(oh*a/2)*2:360' \
115 -frames:v 1 -crf 40 \
116 wip/fav/"$base".avif
117 mv_ {wip/,}fav/"$base".avif
118 }
119 need_opus () {
120 test tmp/fav/"$base".opus -nt src/"$src" || {
121 ffmpeg_ -i src/"$src" \
122 -map 0:a -c:a libopus -b:a 64k -application voip \
123 wip/fav/"$base".opus
124 mv_ {wip,tmp}/fav/"$base".opus
125 }
126 }
127 test fav/"$base".360p.av1.webm -nt src/"$src" || {
128 need_opus
129 ffmpeg_ -i src/"$src" -i tmp/fav/"$base".opus \
130 -map 0:v \
131 -filter:v format=yuv420p,scale='trunc(oh*a/2)*2:360' \
132 -c:v libsvtav1 -crf 40 -g 120 \
133 -map 1:a -c:a copy \
134 wip/fav/"$base".360p.av1.webm
135 mv_ {wip/,}fav/"$base".360p.av1.webm
136 }
137 dst="$name".360p.av1.webm
138 ln -fs -t fav/"$month"/by-uuid/"$uuid" ../../"$dst"
139 #test fav/"$base".360p.vp9.webm -nt src/"$src" || {
140 # need_opus
141 # # See https://developers.google.com/media/vp9/settings/vod#recommended_settings
142 # # See https://gist.github.com/mrintrepide/3033c35ee9557e66cff7806f48dbd339
143 # set -- \
144 # -filter:v format=yuv420p,scale='trunc(oh*a/2)*2:360' \
145 # -c:v libvpx-vp9 -quality good -crf 20 \
146 # -minrate 138k -b:v 276k -maxrate 400k \
147 # -cpu-used 4 -static-thresh 0 -tile-columns 0 -tile-rows 0 -frame-parallel 0 \
148 # -row-mt 1 -aq-mode 0 -auto-alt-ref 6 -lag-in-frames 25 -enable-tpl 1
149 # test -e tmp/fav/"$base".360p.vp9.webm-0.log || {
150 # # FIXME: how to disable h264 debug log?
151 # ffmpeg 2>/dev/null -i src/"$src" \
152 # -map 0:v "$@" -pass 1 -passlogfile wip/fav/"$base".360p.vp9.webm \
153 # -f null \
154 # /dev/null
155 # mv_ -t tmp/fav/"${base%/*}" wip/fav/"$base".360p.vp9.webm-*.log
156 # }
157 # ffmpeg -i src/"$src" -i tmp/fav/"$base".opus \
158 # -map 0:v "$@" -pass 2 -passlogfile tmp/fav/"$base".360p.vp9.webm \
159 # -map 1:a -c:a copy \
160 # wip/fav/"$base".360p.vp9.webm
161 # mkdir -p fav/"$month"/by-uuid/"$uuid"
162 # mv_ wip/fav/"$base".360p.vp9.webm fav/"$base".360p.vp9.webm
163 # rm -fv tmp/fav/"$base".360p.vp9.webm-*.log
164 #}
165 #test fav/"$base".mp4 -nt src/"$src" || {
166 # ffmpeg -i src/"$src" \
167 # -filter:v format=yuv420p \
168 # -c:v libx264 -preset medium -crf 22 \
169 # -tune zerolatency \
170 # -maxrate 1M -bufsize 2M \
171 # -movflags use_metadata_tags +faststart \
172 # -c:a libopus -base:a 64k -application voip \
173 # -filter:v "scale=iw/2:ih/2" \
174 # wip/fav/"$base".mp4
175 # mv_ wip/fav/"$base".mp4 fav/"$base".mp4
176 #}
177 #orient=$(ffprobe -v 0 -select_streams v:0 -show_entries stream_side_data=rotation -of default=nw=1:nk=1 src/"$src")
178 # class='orient$orient'
179 echo "<video controls preload=none poster='$name.avif'>"
180 # See https://jakearchibald.com/2022/html-codecs-parameter-for-av1/
181 # and ffmpeg -i fav/$month/foo.av1.webm -c:v copy -bsf:v trace_headers -f null /dev/null |&
182 # grep -e seq_profile -e seq_level_idx -e seq_tier -e high_bitdepth -e twelve_bit
183 P=0; LL=01; T=M; DD=08
184 echo "<source type='video/webm; codecs=av01.$P.$LL$T.$DD' src='$name.360p.av1.webm' />"
185 echo "<p><a href='$name.360p.av1.webm'>$name.360p.av1.webm</a></p>"
186 echo "</video>"
187 esac
188 touch -a src/"$base".txt
189 printf %s "<span class='comment'>"
190 sed -e '$q;s/$/<br \/>\n/' src/"$base".txt
191 echo "</span>"
192 echo "</div>"
193 echo "<span class='infos'>"
194 echo " <span class='creationDate'>$creationDate</span>"
195 echo "</span>"
196 echo "<span class='links'>"
197 echo " <a class='by-uuid' href='by-uuid/$uuid/$dst' title='Lien public vers cette capture' target='_blank'>@</a>"
198 echo " <a class='anchor' href='#$id' title='Ancre vers cette capture'>#</a>"
199 echo "</span>"
200 echo "</li>"
201 echo
202 done
203 cat <<EOF
204 </ul>
205 </div>
206
207 </body>
208 </html>
209 EOF
210 } >tmp/fav/"$month"/index.html
211 mv_ -f {tmp/,}fav/"$month"/index.html
212
213 echo >&2 "$dir: removing any deleted favorites"
214 (cd fav; find "$month" -mindepth 1 -type f -not -name index.html) |
215 while read -r fav; do
216 base=${fav%.fav.*}
217 name=${base##*/}
218 unset hasSrc
219 for src in src/"$base".fav.*; do test "${src%.txt}" != "$src" || hasSrc=set; done
220 test "${hasSrc:+set}" || {
221 echo >&2 "$dir: removing $year/fav/$fav"
222 rm -f fav/"$fav"
223 rm -f fav/"$month"/by-uuid/*/"${name%.*}".*
224 }
225 done
226 rmdir -p 2>/dev/null fav/"$month"/by-uuid/*/ || true
227 rmdir -p 2>/dev/null wip/fav/* || true
228 popd >/dev/null
229 done