From 9f3a55cf354a3441787d2da0827168d0266bb3b3 Mon Sep 17 00:00:00 2001 From: Levon Gevorgyan Date: Mon, 17 Jun 2024 17:26:49 -0500 Subject: [PATCH] switch to psy and better encode helper --- scripts/build.sh | 107 ++++++++++++++++++++++++----------------- scripts/recc_encode.sh | 55 +++++++++++++++------ 2 files changed, 103 insertions(+), 59 deletions(-) diff --git a/scripts/build.sh b/scripts/build.sh index ec42910..ac6ca4d 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,11 +1,13 @@ #!/bin/bash usage() { - echo "./scripts/build.sh [-h] [-p] [-o] [-r] [-O n]" + echo "./scripts/build.sh [-h] [-A] [-s] [-o] [-r] [-O n]" echo -e "\th: display this help output" - echo -e "\tp: build svt-av1-psy with dovi library" - echo -e "\to: build other encoders x264/5 and vpx" - echo -e "\tr: build rockchip media libraries" + echo -e "\tA: build all AV1 encoders (default is only svt-av1-psy)" + echo -e "\ts: build svt-av1 (default is svt-av1-psy)" + echo -e "\to: build other encoders (x264/5 and vpx)" + echo -e "\tr: build rockchip media libraries" + echo -e "\tv: build libvmaf" echo -e "\tO n: build at optimization n (1, 2, 3)" } @@ -27,6 +29,18 @@ while getopts "$OPTS" flag; do export BUILD_PSY="true" echo "building psy" ;; + A) + export BUILD_ALL_AV1="true" + echo "building all other av1 encoders" + ;; + s) + export BUILD_SVT="true" + echo "building svt-av1" + ;; + v) + export BUILD_VMAF="true" + echo "building libvmaf" + ;; o) export BUILD_OTHERS="true" GREP_FILTER+="|x26|libvpx" @@ -100,6 +114,9 @@ VPX_DIR="$BASE_DIR/vpx" # save options use echo "$@" > "$BASE_DIR/.last_opts" +# build with psy as default +export BUILD_PSY="true" + export ARCH=$(uname -m) export COMP_FLAGS="" if [[ "$ARCH" == "x86_64" ]] @@ -220,47 +237,51 @@ else sudo make install || exit fi -# build rav1e -cd "$RAV1E_DIR/" || exit -update_git -rm -rf ffmpeg_build.user && mkdir ffmpeg_build.user || exit -source "$HOME/.cargo/env" # for good measure -cargo clean -RUSTFLAGS="-C target-cpu=native" cargo cinstall --release \ - --prefix="$(pwd)"/ffmpeg_build.user \ - --libdir="$(pwd)"/ffmpeg_build.user/lib \ - --includedir="$(pwd)"/ffmpeg_build.user/include || exit -cd ffmpeg_build.user || exit -sudo cp ./lib/* /usr/local/lib/ -r || exit -sudo cp ./include/* /usr/local/include/ -r || exit +if [[ "$BUILD_ALL_AV1" == "true" ]]; then + # build rav1e + cd "$RAV1E_DIR/" || exit + update_git + rm -rf ffmpeg_build.user && mkdir ffmpeg_build.user || exit + source "$HOME/.cargo/env" # for good measure + cargo clean + RUSTFLAGS="-C target-cpu=native" cargo cinstall --release \ + --prefix="$(pwd)"/ffmpeg_build.user \ + --libdir="$(pwd)"/ffmpeg_build.user/lib \ + --includedir="$(pwd)"/ffmpeg_build.user/include || exit + cd ffmpeg_build.user || exit + sudo cp ./lib/* /usr/local/lib/ -r || exit + sudo cp ./include/* /usr/local/include/ -r || exit -# build aom -cd "$AOM_DIR/" || exit -update_git -rm -rf build_aom.user -mkdir build_aom.user -cd build_aom.user || exit -make clean -cmake .. -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON \ - -DENABLE_TESTS=OFF \ - -DCMAKE_C_FLAGS="-flto -O${OPT_LVL} $COMP_FLAGS" \ - -DCMAKE_CXX_FLAGS="-flto -O${OPT_LVL} $COMP_FLAGS" || exit -make -j"$(nproc)" || exit -sudo make install || exit + # build aom + cd "$AOM_DIR/" || exit + update_git + rm -rf build_aom.user + mkdir build_aom.user + cd build_aom.user || exit + make clean + cmake .. -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON \ + -DENABLE_TESTS=OFF \ + -DCMAKE_C_FLAGS="-flto -O${OPT_LVL} $COMP_FLAGS" \ + -DCMAKE_CXX_FLAGS="-flto -O${OPT_LVL} $COMP_FLAGS" || exit + make -j"$(nproc)" || exit + sudo make install || exit +fi -# build libvmaf -cd "$VMAF_DIR/libvmaf" || exit -update_git -python3 -m virtualenv .venv -source .venv/bin/activate -rm -rf build.user -mkdir build.user -cd build.user || exit -pip install meson -meson setup ../ build.user --buildtype release -Denable_float=true -Db_lto=true \ - --optimization="$OPT_LVL" -Dc_args="$COMP_FLAGS" -Dcpp_args="$COMP_FLAGS" || exit -ninja -vC build.user || exit -sudo ninja -vC build.user install || exit +if [[ "$BUILD_VMAF" == "true" ]]; then + # build libvmaf + cd "$VMAF_DIR/libvmaf" || exit + update_git + python3 -m virtualenv .venv + source .venv/bin/activate + rm -rf build.user + mkdir build.user + cd build.user || exit + pip install meson + meson setup ../ build.user --buildtype release -Denable_float=true -Db_lto=true \ + --optimization="$OPT_LVL" -Dc_args="$COMP_FLAGS" -Dcpp_args="$COMP_FLAGS" || exit + ninja -vC build.user || exit + sudo ninja -vC build.user install || exit +fi # build dav1d cd "$DAV1D_DIR" || exit diff --git a/scripts/recc_encode.sh b/scripts/recc_encode.sh index c7d3c88..e45f665 100755 --- a/scripts/recc_encode.sh +++ b/scripts/recc_encode.sh @@ -13,31 +13,54 @@ usage() { encode() { ENCODE_FILE="/tmp/encode.sh" - SVT_PARAMS="${GRAIN}tune=0:enable-overlays=1:scd=1:enable-hdr=1:fast-decode=1:enable-variance-boost=1:enable-qm=1:qm-min=0:qm-max=15" + echo '#!/bin/bash' > "$ENCODE_FILE" + + SVT_PARAMS="${GRAIN}adaptive-film-grain=1:tune=3:enable-overlays=1:scd=1:enable-hdr=1:fast-decode=1:enable-variance-boost=1:enable-qm=1:qm-min=0:qm-max=15" + echo "export SVT_PARAMS=\"$SVT_PARAMS\"" >> "$ENCODE_FILE" + UNMAP=$(unmap_streams "$INPUT") + echo "export UNMAP=\"$UNMAP\"" >> "$ENCODE_FILE" + AUDIO_FORMAT='-af "aformat=channel_layouts=7.1|5.1|stereo|mono" -c:a libopus' + echo "export AUDIO_FORMAT='$AUDIO_FORMAT'" >> "$ENCODE_FILE" + AUDIO_BITRATE=$(get_bitrate_audio "$INPUT") + echo "export AUDIO_BITRATE=\"$AUDIO_BITRATE\"" >> "$ENCODE_FILE" + VIDEO_ENCODER="libsvtav1" + echo "export VIDEO_ENCODER=\"$VIDEO_ENCODER\"" >> "$ENCODE_FILE" + VIDEO_PARAMS="-pix_fmt yuv420p10le -crf 25 -preset 3 -g 240" - FFMPEG_PARAMS="-y -c:s copy -c:V $VIDEO_ENCODER $VIDEO_PARAMS" - NL=' \\\n\t' + echo "export VIDEO_PARAMS=\"$VIDEO_PARAMS\"" >> "$ENCODE_FILE" + + FFMPEG_PARAMS="-y -c:s copy -c:V \$VIDEO_ENCODER \$VIDEO_PARAMS" + echo "export FFMPEG_PARAMS=\"$FFMPEG_PARAMS\"" >> "$ENCODE_FILE" FFMPEG_VERSION="$(ffmpeg -version 2>&1 | grep version | cut -d' ' -f1-3)" - VIDEO_ENC_VERSION="$(ldd $(which ffmpeg) | grep -i "$VIDEO_ENCODER" | cut -d' ' -f3 | xargs readlink)" - AUDIO_ENC_VERSION="$(ldd $(which ffmpeg) | grep -i libopus | cut -d' ' -f3 | xargs readlink)" - ADD_METADATA="-metadata encoding_params=\"$FFMPEG_VERSION $AUDIO_ENC_VERSION $VIDEO_ENC_VERSION $VIDEO_PARAMS $SVT_PARAMS\"" + echo "export FFMPEG_VERSION=\"$FFMPEG_VERSION\"" >> "$ENCODE_FILE" + + VIDEO_ENC_VERSION="$(ldd $(which ffmpeg) | grep -i "$VIDEO_ENCODER" | cut -d' ' -f3 | xargs readlink)" + echo "export VIDEO_ENC_VERSION=\"$VIDEO_ENC_VERSION\"" >> "$ENCODE_FILE" + + AUDIO_ENC_VERSION="$(ldd $(which ffmpeg) | grep -i libopus | cut -d' ' -f3 | xargs readlink)" + echo "export AUDIO_ENC_VERSION=\"$AUDIO_ENC_VERSION\"" >> "$ENCODE_FILE" + + ADD_METADATA="\"encoding_params=\\\"\$FFMPEG_VERSION \$AUDIO_ENC_VERSION \$VIDEO_ENC_VERSION \$VIDEO_PARAMS \$SVT_PARAMS\\\"\"" + # ADD_METADATA="\"-metadata encoding_params='yes'\"" + echo "export ADD_METADATA=$ADD_METADATA" >> "$ENCODE_FILE" + + NL=' \\\n\t' - echo '#!/bin/bash' > "$ENCODE_FILE" echo -e ffmpeg -i \""$INPUT"\" -map 0 $UNMAP \ - $AUDIO_FORMAT $NL $AUDIO_BITRATE \ - $ADD_METADATA $NL \ - "$FFMPEG_PARAMS" -dolbyvision 1 -svtav1-params \ - $NL "\"$SVT_PARAMS\" \"$OUTPUT\" ||" $NL \ - ffmpeg -i \""$INPUT"\" -map 0 $UNMAP \ - $AUDIO_FORMAT $NL $AUDIO_BITRATE \ - $ADD_METADATA $NL \ - "$FFMPEG_PARAMS" -svtav1-params \ - $NL "\"$SVT_PARAMS\" \"$OUTPUT\" || exit 1" >> "$ENCODE_FILE" + \$AUDIO_FORMAT $NL \$AUDIO_BITRATE \ + -metadata \"\$ADD_METADATA\" $NL \ + \$FFMPEG_PARAMS -dolbyvision 1 -svtav1-params \ + $NL "\"\$SVT_PARAMS\" \"\$OUTPUT\" ||" $NL \ + ffmpeg -i \""$INPUT"\" -map 0 \$UNMAP \ + \$AUDIO_FORMAT $NL \$AUDIO_BITRATE \ + -metadata \"\$ADD_METADATA\" $NL \ + "\$FFMPEG_PARAMS" -svtav1-params \ + $NL "\"\$SVT_PARAMS\" \"$OUTPUT\" || exit 1 " >> "$ENCODE_FILE" if [[ "$EXT" == "mkv" ]]; then echo "mkvpropedit \"$OUTPUT\" --add-track-statistics-tags" >> "$ENCODE_FILE"