]> Git — Sourcephile - julm/camera.git/blob - reencode.sh
julm/pull: support SD card 6365-3561
[julm/camera.git] / reencode.sh
1 #!/usr/bin/env bash
2 # Copyright: Julien Moutinho <julm+camera@sourcephile.fr>
3 # License: AGPL-3.0-or-later
4 #
5
6 cd "${0%/*}"
7 set -eu
8 shopt -s nullglob
9
10 ffmpeg_ () { local -; set -x; nice -19 time ffmpeg </dev/null -hide_banner -loglevel warning -stats -y "$@"; }
11 mv_ () { local -; mv -i >&2 "$@"; }
12 set -x
13 for dir in "$@"; do
14 IFS=/ read -r user year x month x <<<"$dir"
15 test -n "$year" || exec "$0" "$user"/2*/src/*/
16 test -n "$month" || exec "$0" "$user"/"$year"/src/*/
17 pushd "$user/$year" >/dev/null
18 test -d src/"$month" || { popd; continue; }
19 (cd src; find -L "$month" -type f -size +180M -printf '%f %p\n') | sort -t/ -k1.5 | cut -f2 -d ' ' |
20 while read -r src; do
21 echo >&2 "$dir: processing $year/src/$src"
22 base=${src%.*}
23 name=${base#*/}
24 id=${name%%.*}
25 mkdir -p {{tmp,wip}/reencode,old}/"${base%/*}"
26 need_psize () {
27 # Slow, but needed to make wide videos span two columns of the CSS grid
28 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")"
29 case "$rotation" in
30 (90|-90) w=$width; width=$height; height=$w;;
31 esac
32 if [ "$width" -gt "$height" ]
33 then psize=$height
34 else psize=$width
35 fi
36 }
37 case "$src" in
38 *.mp4|*.MOV)
39 need_psize
40 case $psize in
41 480) crf=30;;
42 720) crf=40;;
43 1080) crf=50;;
44 *) exit 3;;
45 esac
46 preset=5
47 ext="preset=$preset.crf=$crf.av1.webm"
48 need_opus () {
49 test tmp/reencode/"$base".opus -nt src/"$src" || {
50 ffmpeg_ -i src/"$src" \
51 -map 0:a -c:a libopus -b:a 64k -application voip \
52 wip/reencode/"$base".opus
53 mv_ {wip,tmp}/reencode/"$base".opus
54 }
55 }
56 test src/"$base.$ext" -nt src/"$src" || {
57 need_opus
58 ffmpeg_ -i src/"$src" -i tmp/reencode/"$base".opus \
59 -map 0:v \
60 -filter:v format=yuv420p \
61 -c:v libsvtav1 -preset "$preset" -crf "$crf" \
62 -svtav1-params input-depth=8:keyint=10s:enable-overlays=1:enable-tf=0:scd=1:scm=0:tune=0:fast-decode=1 \
63 -map 1:a -c:a copy \
64 wip/reencode/"$base.$ext"
65 mv_ {wip/reencode,src}/"$base.$ext"
66 mv_ {src,old}/"$src"
67 }
68 ;;
69 esac
70 done
71 rm -vrf {tmp,wip}/reencode
72 popd >/dev/null
73 done