#!/usr/bin/env bash # Copyright: Julien Moutinho # License: AGPL-3.0-or-later # cd "${0%/*}" set -eu shopt -s nullglob ffmpeg_ () { local -; set -x; nice -19 ffmpeg &2 "$@"; } set -x for dir in "$@"; do IFS=/ read -r user year x month x <<<"$dir" test -n "$year" || exec "$0" "$user"/2*/src/*/ test -n "$month" || exec "$0" "$user"/"$year"/src/*/ pushd "$user/$year" >/dev/null test -d src/"$month" || { popd; continue; } (cd src; find -L "$month" -type f -size +200M -printf '%f %p\n') | sort -t/ -k1.5 | cut -f2 -d ' ' | while read -r src; do echo >&2 "$dir: processing $year/src/$src" base=${src%.*} name=${base#*/} id=${name%%.*} mkdir -p {tmp,wip,old}/reencode/"${base%/*}" case "$src" in *.mp4) # Slow, but needed to make wide videos span two columns of the CSS grid orient=$(ffprobe -v 0 -select_streams v:0 -show_entries stream_side_data=rotation -of default=nw=1:nk=1 src/"$src") psize=720 crf=40 preset=7 ext="preset=$preset.crf=$crf.${psize}p.av1.webm" if [ -z "$orient" ]; then scale="-2:${psize}"; else scale="${psize}:-2"; fi need_opus () { test tmp/reencode/"$base".opus -nt src/"$src" || { ffmpeg_ -i src/"$src" \ -map 0:a -c:a libopus -b:a 64k -application voip \ wip/reencode/"$base".opus mv_ {wip,tmp}/reencode/"$base".opus } } test src/"$base.$ext" -nt src/"$src" || { need_opus # See https://gitlab.com/AOMediaCodec/SVT-AV1/-/blob/master/Docs/CommonQuestions.md # See https://gist.github.com/BlueSwordM/86dfcb6ab38a93a524472a0cbe4c4100 # See https://www.streamingmediaglobal.com/Articles/ReadArticle.aspx?ArticleID=154488 # -preset Range from 0 to 13, with higher numbers providing a higher encoding speed; # -crf Range is 0-63, with the default being 50. # Lower values correspond to higher quality and greater file size # yuv420p # yuv420p10le can represent more shades of grey and colors # and is less prone to certain artifacts, such as color # banding and loss of detail in low luma areas, # cost in terms of resulting file size (~5%), # can also be more compute-intensive than 8-bit in some decoders # tune=0 subjective mode often results in an image with greater # sharpness and is intended to produce a result that appears to humans # to be of high quality (as opposed to doing well on basic objective measures, # such as PSNR) # scd=1 biases the encoders' scene detection to insert more intra refreshes if needed, # and reduce temporal dependencies around scene-changes. # scm=0 screen content mode decision making. 0 is best for live-action. # fast-decode=3 trade compression efficiency for less CPU time when decoding # for preset 5-7: levels 1-3 (3 being faster) are supported. # FIXME: set fast-decode to 3 available on newer svt-av1 ffmpeg_ -i src/"$src" -i tmp/reencode/"$base".opus \ -map 0:v \ -filter:v format=yuv420p,scale="$scale" \ -c:v libsvtav1 -preset "$preset" -crf "$crf" \ -svtav1-params tune=0:enable-overlays=1:scd=1:scm=0:fast-decode=1 \ -map 1:a -c:a copy \ wip/reencode/"$base.$ext" mv_ {wip/reencode,src}/"$base.$ext" mv_ {src,old/reencode}/"$src" } ;; esac done rm -vrf {tmp,wip}/reencode popd >/dev/null done