]> Git — Sourcephile - julm/camera.git/blob - reencode.sh
reencode: init
[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 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 +200M -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,old}/reencode/"${base%/*}"
26 case "$src" in
27 *.mp4)
28 # Slow, but needed to make wide videos span two columns of the CSS grid
29 orient=$(ffprobe -v 0 -select_streams v:0 -show_entries stream_side_data=rotation -of default=nw=1:nk=1 src/"$src")
30 psize=720
31 crf=40
32 ext="crf=$crf.${psize}p.av1.webm"
33 if [ -z "$orient" ]; then scale="-2:${psize}"; else scale="${psize}:-2"; fi
34 need_opus () {
35 test tmp/reencode/"$base".opus -nt src/"$src" || {
36 ffmpeg_ -i src/"$src" \
37 -map 0:a -c:a libopus -b:a 64k -application voip \
38 wip/reencode/"$base".opus
39 mv_ {wip,tmp}/reencode/"$base".opus
40 }
41 }
42 test src/"$base.$ext" -nt src/"$src" || {
43 need_opus
44 # See https://gitlab.com/AOMediaCodec/SVT-AV1/-/blob/master/Docs/CommonQuestions.md
45 # See https://gist.github.com/BlueSwordM/86dfcb6ab38a93a524472a0cbe4c4100
46 # See https://www.streamingmediaglobal.com/Articles/ReadArticle.aspx?ArticleID=154488
47 # -preset Range from 0 to 13, with higher numbers providing a higher encoding speed;
48 # -crf Range is 0-63, with the default being 50.
49 # Lower values correspond to higher quality and greater file size
50 # yuv420p
51 # yuv420p10le can represent more shades of grey and colors
52 # and is less prone to certain artifacts, such as color
53 # banding and loss of detail in low luma areas,
54 # cost in terms of resulting file size (~5%),
55 # can also be more compute-intensive than 8-bit in some decoders
56 # tune=0 subjective mode often results in an image with greater
57 # sharpness and is intended to produce a result that appears to humans
58 # to be of high quality (as opposed to doing well on basic objective measures,
59 # such as PSNR)
60 # scd=1 biases the encoders' scene detection to insert more intra refreshes if needed,
61 # and reduce temporal dependencies around scene-changes.
62 # scm=0 screen content mode decision making. 0 is best for live-action.
63 # fast-decode=3 trade compression efficiency for less CPU time when decoding
64 # for preset 5-7: levels 1-3 (3 being faster) are supported.
65 ffmpeg_ -i src/"$src" -i tmp/reencode/"$base".opus \
66 -map 0:v \
67 -filter:v format=yuv420p,scale="$scale" \
68 -c:v libsvtav1 -preset 7 -crf "$crf" \
69 -svtav1-params tune=0:enable-overlays=1:scd=1:scm=0:fast-decode=3 \
70 -map 1:a -c:a copy \
71 wip/reencode/"$base.$ext"
72 mv_ {wip/reencode,src}/"$base.$ext"
73 mv_ {src,old/reencode}/"$src"
74 }
75 ;;
76 esac
77 done
78 rm -vrf {tmp,wip}/reencode
79 popd >/dev/null
80 done