]> Git — Sourcephile - julm/camera.git/blob - index.sh
index: drop attached_pic for WebM which does not support that
[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 2023/src/02/IMG_20230228_111645{,.fav}.jpg
6 # $ echo >2023/src/02/IMG_20230228_111645.fav.txt "Some HTML <b>comment</b> about the picture"
7 # $ years=2023 months=02 ./index.sh
8 # $ sensible-browser 2023/fav/02/index.html
9
10 # shellcheck disable=SC2086
11
12 cd "${0%/*}"
13 set -eu
14 shopt -s nullglob
15
16 ffmpeg_ () { local -; set -x; nice -19 ffmpeg </dev/null -loglevel warning -stats -report -y "$@"; }
17 magick_ () { local -; set -x; nice -19 magick "$@"; }
18 mv_ () { local -; set -x; mv -iv >&2 "$@"; }
19
20 user=$(realpath --relative-to .. .)
21 declare -a years=${years:-$(find * -maxdepth 0 -type d -not -name ".*")}
22 for year in ${years[@]}; do
23 pushd "$year" >/dev/null
24 test -d src || { popd; continue; }
25 mkdir -p fav
26 ln -sf -t fav ../../index.css
27 declare -a months=${months:-(01 02 03 04 05 06 07 08 09 10 11 12)}
28 for month in ${months[@]}; do
29 test -d src/"$month" || { popd; continue; }
30 mkdir -p fav/"$month"
31 mkdir -p {tmp,wip}/fav/"$month"
32 {
33 genDate=$(date +%s)
34 cat <<EOF
35 <!doctype html>
36 <!-- Generator: https://git.sourcephile.fr/julm/camera -->
37 <!-- GenerationDate: $(date -R -d@"$genDate") -->
38 <html>
39
40 <head lang="fr">
41 <meta charset="utf-8">
42 <title>camera/$user/$year/$month</title>
43 <link rel="stylesheet" href="../index.css?$genDate">
44 </head>
45
46 <body>
47
48 <nav class="path">
49 <ul>
50 <li><a href='../../..'>camera</a>/ <a href='../..'>$user</a>/ <a href='..'>$year</a>/ $month</li>
51 </ul></nav>
52
53 <div class="camera">
54 <ul>
55 EOF
56 echo >&2 "$year/$month: processing favorites in chronological order"
57 # Time is encoded in filenames like so: MM/{IMG,VID}_YYYYMMDD_hhmmss.*
58 # hence sorting after the underscore (2nd field, 5th char).
59 (cd src; find "$month" -name '*.fav.*' -not -name "*.txt") | sort -n -t/ -k2.5 |
60 while read -r src; do
61 echo >&2 "$year/$month: processing $year/src/$src"
62 base=${src%.*}
63 name=${base##*/}
64 id=${name%%.*}
65 creationDate=$(printf %s "${src##*/???_}" | sed -e 's/\(....\)\(..\)\(..\)_\(..\)\(..\)\(..\).*/\1-\2-\3 \4:\5:\6/')
66 echo "<li id='$id'><div class='item'>"
67 test -e fav/"$base".uuid ||
68 uuidgen --random >fav/"$base".uuid
69 uuid=$(cat fav/"$base".uuid)
70 mkdir -p fav/"$month"/by-uuid/"$uuid"
71 case "$src" in
72 *.jpg)
73 echo "<picture>"
74 printf "<source type='image/avif' srcset='"
75 for w in {1200,600,300}; do
76 test fav/"$base"."$w"x.avif -nt src/"$src" || {
77 magick_ src/"$src" \
78 -sampling-factor 4:2:0 -colorspace sRGB \
79 -auto-orient -thumbnail "$w"x -unsharp 0x.5 \
80 fav/"$base"."$w"x.avif
81 }
82 printf %s "$name.${w}x.avif ${w}w, "
83 done
84 echo "' />"
85 w=600
86 test fav/"$base"."$w"x.avif -nt src/"$src" || {
87 magick_ -define jpeg:size=$((w * 2))x src/"$src" \
88 -sampling-factor 4:2:0 -interlace JPEG -colorspace sRGB \
89 -auto-orient -thumbnail "$w"x -unsharp 0x.5 \
90 fav/"$base"."$w"x.jpg
91 }
92 echo "<source type='image/jpeg' srcset='$name.${w}x.jpg ${w}w' />"
93 echo "<img src='$name.${w}x.jpg' alt='$creationDate' />"
94 echo "</picture>"
95 dst="$name".1200x.avif
96 ln -fs -t fav/"$month"/by-uuid/"$uuid" ../../"$dst"
97 ;;
98 *.mp4)
99 test fav/"$base".avif -nt src/"$src" || {
100 ffmpeg_ -i src/"$src" \
101 -map 0:v \
102 -filter:v format=yuv420p,scale='trunc(oh*a/2)*2:360' \
103 -frames:v 1 -crf 40 \
104 wip/fav/"$base".avif
105 mv_ {wip/,}fav/"$base".avif
106 }
107 need_opus () {
108 test tmp/fav/"$base".opus -nt src/"$src" || {
109 ffmpeg_ -i src/"$src" \
110 -map 0:a -c:a libopus -b:a 64k -application voip \
111 wip/fav/"$base".opus
112 mv_ {wip,tmp}/fav/"$base".opus
113 }
114 }
115 test fav/"$base".360p.av1.webm -nt src/"$src" || {
116 need_opus
117 ffmpeg_ -i src/"$src" -i tmp/fav/"$base".opus \
118 -map 0:v \
119 -filter:v format=yuv420p,scale='trunc(oh*a/2)*2:360' \
120 -c:v libsvtav1 -crf 40 -g 120 \
121 -map 1:a -c:a copy \
122 wip/fav/"$base".360p.av1.webm
123 mv_ {wip/,}fav/"$base".360p.av1.webm
124 }
125 dst="$name".360p.av1.webm
126 ln -fs -t fav/"$month"/by-uuid/"$uuid" ../../"$dst"
127 #test fav/"$base".360p.vp9.webm -nt src/"$src" || {
128 # need_opus
129 # # See https://developers.google.com/media/vp9/settings/vod#recommended_settings
130 # # See https://gist.github.com/mrintrepide/3033c35ee9557e66cff7806f48dbd339
131 # set -- \
132 # -filter:v format=yuv420p,scale='trunc(oh*a/2)*2:360' \
133 # -c:v libvpx-vp9 -quality good -crf 20 \
134 # -minrate 138k -b:v 276k -maxrate 400k \
135 # -cpu-used 4 -static-thresh 0 -tile-columns 0 -tile-rows 0 -frame-parallel 0 \
136 # -row-mt 1 -aq-mode 0 -auto-alt-ref 6 -lag-in-frames 25 -enable-tpl 1
137 # test -e tmp/fav/"$base".360p.vp9.webm-0.log || {
138 # # FIXME: how to disable h264 debug log?
139 # ffmpeg 2>/dev/null -i src/"$src" \
140 # -map 0:v "$@" -pass 1 -passlogfile wip/fav/"$base".360p.vp9.webm \
141 # -f null \
142 # /dev/null
143 # mv_ -t tmp/fav/"${base%/*}" wip/fav/"$base".360p.vp9.webm-*.log
144 # }
145 # ffmpeg -i src/"$src" -i tmp/fav/"$base".opus \
146 # -map 0:v "$@" -pass 2 -passlogfile tmp/fav/"$base".360p.vp9.webm \
147 # -map 1:a -c:a copy \
148 # wip/fav/"$base".360p.vp9.webm
149 # mkdir -p fav/"$month"/by-uuid/"$uuid"
150 # mv_ wip/fav/"$base".360p.vp9.webm fav/"$base".360p.vp9.webm
151 # rm -fv tmp/fav/"$base".360p.vp9.webm-*.log
152 #}
153 #test fav/"$base".mp4 -nt src/"$src" || {
154 # ffmpeg -i src/"$src" \
155 # -filter:v format=yuv420p \
156 # -c:v libx264 -preset medium -crf 22 \
157 # -tune zerolatency \
158 # -maxrate 1M -bufsize 2M \
159 # -movflags use_metadata_tags +faststart \
160 # -c:a libopus -base:a 64k -application voip \
161 # -filter:v "scale=iw/2:ih/2" \
162 # wip/fav/"$base".mp4
163 # mv_ wip/fav/"$base".mp4 fav/"$base".mp4
164 #}
165 #orient=$(ffprobe -v 0 -select_streams v:0 -show_entries stream_side_data=rotation -of default=nw=1:nk=1 src/"$src")
166 # class='orient$orient'
167 echo "<video controls preload=none poster='$name.avif'>"
168 # See https://jakearchibald.com/2022/html-codecs-parameter-for-av1/
169 # and ffmpeg -i fav/$month/foo.av1.webm -c:v copy -bsf:v trace_headers -f null /dev/null |&
170 # grep -e seq_profile -e seq_level_idx -e seq_tier -e high_bitdepth -e twelve_bit
171 P=0; LL=01; T=M; DD=08
172 echo "<source type='video/webm; codecs=av01.$P.$LL$T.$DD' src='$name.360p.av1.webm' />"
173 echo "<p><a href='$name.360p.av1.webm'>$name.360p.av1.webm</a></p>"
174 echo "</video>"
175 esac
176 touch -a src/"$base".txt
177 printf %s "<span class='comment'>"
178 sed -e '$q;s/$/<br \/>\n/' src/"$base".txt
179 echo "</span>"
180 echo "</div>"
181 echo "<span class='infos'>"
182 echo " <span class='creationDate'>$creationDate</span>"
183 echo "</span>"
184 echo "<span class='links'>"
185 echo " <a class='anchor' href='#$id' title='Ancre vers cette capture'>#</a>"
186 echo " <a class='by-uuid' href='by-uuid/$uuid/$dst' title='Lien public vers cette capture' target='_blank'>@</a>"
187 echo "</span>"
188 echo "</li>"
189 echo
190 done
191 cat <<EOF
192 </ul>
193 </div>
194
195 </body>
196 </html>
197 EOF
198 } >tmp/fav/"$month"/index.html
199 mv_ -f {tmp/,}fav/"$month"/index.html
200
201 echo >&2 "$year/$month: remove any deleted favorites"
202 (cd fav; find "$month" -mindepth 1 -type f -not -name index.html) |
203 while read -r fav; do
204 base=${fav%.fav.*}
205 name=${base##*/}
206 unset hasSrc
207 for src in src/"$base".fav.*; do test "${src%.txt}" != "$src" || hasSrc=set; done
208 test "${hasSrc:+set}" || {
209 echo >&2 "$year/$month: removing $year/fav/$fav"
210 rm -f fav/"$fav"
211 rm -f fav/"$month"/by-uuid/*/"${name%.*}".*
212 }
213 done
214 rmdir -p 2>/dev/null fav/"$month"/by-uuid/*/ || true
215 done
216 popd >/dev/null
217 done
218 rmdir 2>/dev/null -p wip/fav/* || true