]> Git — Sourcephile - julm/camera.git/blob - reencode.sh
sevy/pull: update to use julm/pull's logic
[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 cat month x <<<"$dir"
15 listing=
16 test -f "$dir" || listing=1
17 if test -n "$listing"
18 then
19 test -n "$year" || exec "$0" "$user"/2*/src/*/
20 test -n "$month" || exec "$0" "$user"/"$year"/src/*/
21 pushd "$user/$year" >/dev/null
22 test -d src/"$month" || { popd; continue; }
23 else
24 pushd "$user/$year" >/dev/null
25 fi
26 if test -n "$listing"
27 then (cd src; find -L "$month" -type f -size +100M -printf '%f %p\n') | sort -t/ -k1.5 | cut -f2 -d ' '
28 else printf '%s\n' "${dir#"$user/$year/$cat/"}"
29 fi |
30 while read -r src; do
31 echo >&2 "$dir: processing $user/$year/src/$src"
32 base=${src%.*}
33 name=${base#*/}
34 id=${name%%.*}
35 mkdir -p {{tmp,wip}/reencode,old}/"${base%/*}"
36 need_psize () {
37 # Slow, but needed to make wide videos span two columns of the CSS grid
38 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")"
39 case "$rotation" in
40 (90|-90) w=$width; width=$height; height=$w;;
41 esac
42 if [ "$width" -gt "$height" ]
43 then psize=$height
44 else psize=$width
45 fi
46 }
47 case "$src" in
48 *.mp4|*.MOV)
49 need_psize
50 case $psize in
51 480) crf=30;;
52 720) crf=40;;
53 1080) crf=50;;
54 *) exit 3;;
55 esac
56 preset=5
57 ext="preset=$preset.crf=$crf.av1.webm"
58 need_opus () {
59 test tmp/reencode/"$base".opus -nt src/"$src" || {
60 ffmpeg_ -i src/"$src" \
61 -map 0:a -c:a libopus -b:a 64k -application voip \
62 wip/reencode/"$base".opus
63 mv_ {wip,tmp}/reencode/"$base".opus
64 }
65 }
66 test src/"$base.$ext" -nt src/"$src" || {
67 need_opus
68 ffmpeg_ -i src/"$src" -i tmp/reencode/"$base".opus \
69 -map 0:v \
70 -filter:v format=yuv420p \
71 -c:v libsvtav1 -preset "$preset" -crf "$crf" \
72 -svtav1-params input-depth=8:keyint=10s:enable-overlays=1:enable-tf=0:scd=1:scm=0:tune=0:fast-decode=1 \
73 -map 1:a -c:a copy \
74 wip/reencode/"$base.$ext"
75 mv_ {wip/reencode,src}/"$base.$ext"
76 mv_ {src,old}/"$src"
77 }
78 ;;
79 esac
80 done
81 rm -vrf {tmp,wip}/reencode
82 popd >/dev/null
83 done