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