From 84dc0d1e64d43b99d673d9a00517e5fc08187f86 Mon Sep 17 00:00:00 2001 From: Levon Date: Sat, 13 Apr 2024 16:51:46 -0500 Subject: [PATCH 1/7] verson --- scripts/benchmark.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/benchmark.sh b/scripts/benchmark.sh index e267334..3235e30 100755 --- a/scripts/benchmark.sh +++ b/scripts/benchmark.sh @@ -57,7 +57,7 @@ PRESET=(8) LOG="$BENCHMARK_DIR/results.txt" CSV="$BENCHMARK_DIR/results.csv" rm -rf "$OUTPUT_DIR" && mkdir -p "$OUTPUT_DIR" -ffmpeg -version | grep "version" > "$LOG" +ffmpeg -version | grep -E "version|built|configuration" > "$LOG" echo "ENCODER,PRESET,CRF,INPUT,TIME_TAKEN,SIZE,PSNR_HVS,CAMBI,FLOAT_MS_SSIM,VMAF" > "$CSV" uname -srmpio >> "$LOG" CPU_PROD=$(sudo lshw | grep "product" | head -1 | cut -d ':' -f2) From c73469a0d96930194080699dbcc8d98df9f8393f Mon Sep 17 00:00:00 2001 From: Levon Date: Tue, 16 Apr 2024 08:18:37 -0500 Subject: [PATCH 2/7] wip recc_encode --- .gitignore | 2 +- scripts/recc_encode.sh | 36 ++++++++++++++++++++++++++++++++++ scripts/recc_encode_install.sh | 3 +++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100755 scripts/recc_encode.sh create mode 100755 scripts/recc_encode_install.sh diff --git a/.gitignore b/.gitignore index e6f34ce..b2af0ae 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,4 @@ benchmark graphs rkmpp rkrga - +x265* diff --git a/scripts/recc_encode.sh b/scripts/recc_encode.sh new file mode 100755 index 0000000..e3c8677 --- /dev/null +++ b/scripts/recc_encode.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +# this is simply my recommended encoding method. +# do not take this as a holy grail. + +encode() { + FILENAME="$1" + OUTPUT_NAME="" + # allow optional output filename + if [[ -n "$2" ]]; + then + OUTPUT_NAME="$2" + else + OUTPUT_NAME="${HOME}/av1_${FILENAME}" + fi + + echo ffmpeg -i \""$FILENAME"\" -map 0 \ + -af '"aformat=channel_layouts=7.1|5.1|stereo|mono"' -c:a libopus $(get_bitrate_audio "$FILENAME") \ + -c:s copy -c:v libsvtav1 -pix_fmt yuv420p10le -crf 20 -preset 3 -g 240 \ + -svtav1-params \"tune=0:enable-overlays=1:scd=1:enable-hdr=1:fast-decode=1:enable-variance-boost=1\" \ + \""$OUTPUT_NAME"\" +} + +get_bitrate_audio() { + FILENAME="$1" + bitrate_cmd="" + num_streams=$(ffprobe -v error -select_streams a -show_entries stream=index -of csv=p=0 "$FILENAME" | wc -l) + for ((i = 0; i < num_streams; i++)); do + num_channels=$(ffprobe -v error -select_streams "a:$i" -show_entries stream=channels -of default=noprint_wrappers=1:nokey=1 "$FILENAME") + bitrate=$((num_channels * 64)) + bitrate_cmd+="-b:a:$i ${bitrate}k " + done + echo "$bitrate_cmd" +} + +encode "$@" diff --git a/scripts/recc_encode_install.sh b/scripts/recc_encode_install.sh new file mode 100755 index 0000000..febc10c --- /dev/null +++ b/scripts/recc_encode_install.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +sudo ln -sf "$(pwd)/scripts/recc_encode.sh" /usr/local/bin/encode \ No newline at end of file From f5e2b55c41c585d6d6c3763d2a432c5b80dcce84 Mon Sep 17 00:00:00 2001 From: Levon Date: Wed, 17 Apr 2024 17:44:21 -0500 Subject: [PATCH 3/7] encode things --- scripts/analyze.sh | 2 +- scripts/benchmark.sh | 2 +- scripts/install_deps.sh | 2 +- scripts/recc_encode.sh | 81 ++++++++++++++++++++++++-------- scripts/recc_encode_install.sh | 2 +- scripts/recc_encode_uninstall.sh | 3 ++ 6 files changed, 69 insertions(+), 23 deletions(-) create mode 100755 scripts/recc_encode_uninstall.sh diff --git a/scripts/analyze.sh b/scripts/analyze.sh index 9068cce..9866c7e 100755 --- a/scripts/analyze.sh +++ b/scripts/analyze.sh @@ -35,4 +35,4 @@ set style line 1 \ pointtype 7 pointsize 1.5 plot "$RESULTS" using 3:6 with linepoints linestyle 1 -EOF \ No newline at end of file +EOF diff --git a/scripts/benchmark.sh b/scripts/benchmark.sh index 3235e30..fd2c4f5 100755 --- a/scripts/benchmark.sh +++ b/scripts/benchmark.sh @@ -145,4 +145,4 @@ do done echo -e "\n\n--- Results CSV ---\n" >> "$LOG" -cat "$CSV" >> "$LOG" \ No newline at end of file +cat "$CSV" >> "$LOG" diff --git a/scripts/install_deps.sh b/scripts/install_deps.sh index fa9268d..ad3a226 100755 --- a/scripts/install_deps.sh +++ b/scripts/install_deps.sh @@ -7,7 +7,7 @@ COMMON_DEP_NAMES="autoconf automake cmake libtool pkg-config bc texinfo \ APT_DEP_NAMES="build-essential git-core g++-12 libass-dev libfreetype6-dev \ libsdl2-dev libva-dev libvdpau-dev gcc-12 libvorbis-dev libxcb1-dev \ libxcb-shm0-dev libxcb-xfixes0-dev zlib1g-dev libssl-dev ninja-build \ - gobjc++ python3-pip mawk" + gobjc++ python3-pip mawk mediainfo mkvpropedit" PACMAN_DEP_NAMES="base-devel ninja python-pip" diff --git a/scripts/recc_encode.sh b/scripts/recc_encode.sh index e3c8677..a3d137d 100755 --- a/scripts/recc_encode.sh +++ b/scripts/recc_encode.sh @@ -3,34 +3,77 @@ # this is simply my recommended encoding method. # do not take this as a holy grail. +usage() { + echo "unrecognized arguments, please retry" + echo "encode -i input_file [-p] output_file" + echo -e "\t-p print the command instead of executing [optional]" + return 0 +} + encode() { - FILENAME="$1" - OUTPUT_NAME="" - # allow optional output filename - if [[ -n "$2" ]]; - then - OUTPUT_NAME="$2" - else - OUTPUT_NAME="${HOME}/av1_${FILENAME}" - fi - - echo ffmpeg -i \""$FILENAME"\" -map 0 \ - -af '"aformat=channel_layouts=7.1|5.1|stereo|mono"' -c:a libopus $(get_bitrate_audio "$FILENAME") \ - -c:s copy -c:v libsvtav1 -pix_fmt yuv420p10le -crf 20 -preset 3 -g 240 \ + echo ffmpeg -i \""$INPUT"\" -map 0 \ + -af '"aformat=channel_layouts=7.1|5.1|stereo|mono"' -c:a libopus $(get_bitrate_audio "$INPUT") \ + -c:s copy -c:V libsvtav1 -pix_fmt yuv420p10le -crf 25 -preset 3 -g 240 \ -svtav1-params \"tune=0:enable-overlays=1:scd=1:enable-hdr=1:fast-decode=1:enable-variance-boost=1\" \ - \""$OUTPUT_NAME"\" + \""$OUTPUT"\" > /tmp/encode.sh + + if [[ "$PRINT_OUT" == "true" ]]; + then + cat /tmp/encode.sh + else + bash /tmp/encode.sh + fi +} + +unmap_streams(){ + INPUT="$1" + num_video_streams=$(ffprobe -v error -select_streams v -show_entries stream=index -of csv=p=0 "$INPUT" | wc -l) + for ((i = 0; i < num_video_streams; i++)); do + ffprobe -v error -select_streams "v:$i" -of default=noprint_wrappers=1:nokey=1 "$INPUT" + ffprobe -select_streams "v:0" -of default=noprint_wrappers=1:nokey=1 'first_20_DN.mkv' + ffprobe -v error -select_streams v -show_entries stream=index:stream_tags=type -of csv=p=0 'first_20_DN.mkv' + done + echo "$num_video_streams" } get_bitrate_audio() { - FILENAME="$1" bitrate_cmd="" - num_streams=$(ffprobe -v error -select_streams a -show_entries stream=index -of csv=p=0 "$FILENAME" | wc -l) - for ((i = 0; i < num_streams; i++)); do - num_channels=$(ffprobe -v error -select_streams "a:$i" -show_entries stream=channels -of default=noprint_wrappers=1:nokey=1 "$FILENAME") + num_audio_streams=$(ffprobe -v error -select_streams a -show_entries stream=index -of csv=p=0 "$INPUT" | wc -l) + for ((i = 0; i < num_audio_streams; i++)); do + num_channels=$(ffprobe -v error -select_streams "a:$i" -show_entries stream=channels -of default=noprint_wrappers=1:nokey=1 "$INPUT") bitrate=$((num_channels * 64)) bitrate_cmd+="-b:a:$i ${bitrate}k " done echo "$bitrate_cmd" } -encode "$@" +# between only 1-4 arguments +test "$#" -eq 0 && usage && exit 1 +test "$#" -gt 4 && usage && exit 1 +while getopts "i:p" flag; do + case "${flag}" in + i) + INPUT="${OPTARG}" + ;; + p) + PRINT_OUT="true" + ;; + *) + usage + exit 1 + ;; + esac +done + +# allow optional output filename +if [[ "$PRINT_OUT" == "true" && "$#" -eq 4 ]]; +then + OUTPUT="${@: -1}" +else + OUTPUT="${HOME}/av1_${INPUT}" +fi + +encode + +# encode "$@" +# unmap_streams "$@" diff --git a/scripts/recc_encode_install.sh b/scripts/recc_encode_install.sh index febc10c..9b3590c 100755 --- a/scripts/recc_encode_install.sh +++ b/scripts/recc_encode_install.sh @@ -1,3 +1,3 @@ #!/bin/bash -sudo ln -sf "$(pwd)/scripts/recc_encode.sh" /usr/local/bin/encode \ No newline at end of file +sudo ln -sf "$(pwd)/scripts/recc_encode.sh" /usr/local/bin/encode diff --git a/scripts/recc_encode_uninstall.sh b/scripts/recc_encode_uninstall.sh new file mode 100755 index 0000000..e714349 --- /dev/null +++ b/scripts/recc_encode_uninstall.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +sudo rm /usr/local/bin/encode From edfd1b0b0d36f40b9ec96974fae42adad0e47365 Mon Sep 17 00:00:00 2001 From: Levon Date: Thu, 18 Apr 2024 07:22:50 -0500 Subject: [PATCH 4/7] mkvtoolnix --- scripts/install_deps.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/install_deps.sh b/scripts/install_deps.sh index ad3a226..ac29a65 100755 --- a/scripts/install_deps.sh +++ b/scripts/install_deps.sh @@ -7,7 +7,7 @@ COMMON_DEP_NAMES="autoconf automake cmake libtool pkg-config bc texinfo \ APT_DEP_NAMES="build-essential git-core g++-12 libass-dev libfreetype6-dev \ libsdl2-dev libva-dev libvdpau-dev gcc-12 libvorbis-dev libxcb1-dev \ libxcb-shm0-dev libxcb-xfixes0-dev zlib1g-dev libssl-dev ninja-build \ - gobjc++ python3-pip mawk mediainfo mkvpropedit" + gobjc++ python3-pip mawk mediainfo mkvtoolnix" PACMAN_DEP_NAMES="base-devel ninja python-pip" From 5830de801f996571c728f1c9c202825ddac28b69 Mon Sep 17 00:00:00 2001 From: Levon Date: Thu, 18 Apr 2024 08:33:18 -0500 Subject: [PATCH 5/7] better opt control --- scripts/benchmark.sh | 4 ++-- scripts/recc_encode.sh | 27 ++++++++++++++++++++------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/scripts/benchmark.sh b/scripts/benchmark.sh index fd2c4f5..12fc31d 100755 --- a/scripts/benchmark.sh +++ b/scripts/benchmark.sh @@ -106,14 +106,14 @@ do elif [[ "$encoder" == "libsvtav1" ]] then PARAMS="-preset $preset -crf $crf -svtav1-params \ - scd=1:tune=0:enable-overlays=1:enable-hdr=1:fast-decode=1 " + scd=1:tune=0:enable-overlays=1:enable-hdr=1:fast-decode=1:enable-variance-boost=1" else PARAMS="" fi # encode export TIMEFORMAT=%R - FFMPEG_CMD="ffmpeg -i $INPUT_DIR/$input -c:a copy -c:v $encoder $PARAMS -pix_fmt yuv420p10le $OUTPUT" + FFMPEG_CMD="ffmpeg -i $INPUT_DIR/$input -pix_fmt yuv420p10le -c:a copy -c:v $encoder $PARAMS $OUTPUT" (time $FFMPEG_CMD) |& tee TIME TIME_DIFF="$(cat TIME | tail -n 1)" rm TIME diff --git a/scripts/recc_encode.sh b/scripts/recc_encode.sh index a3d137d..d8f2256 100755 --- a/scripts/recc_encode.sh +++ b/scripts/recc_encode.sh @@ -47,10 +47,15 @@ get_bitrate_audio() { echo "$bitrate_cmd" } -# between only 1-4 arguments -test "$#" -eq 0 && usage && exit 1 -test "$#" -gt 4 && usage && exit 1 -while getopts "i:p" flag; do + +OPTS='i:p' +NUM_OPTS=$(echo $OPTS | tr ':' '\n' | wc -l) +PRINT_OUT="false" +MIN_OPT=2 +MAX_OPT=4 +test "$#" -lt $MIN_OPT && usage && exit 1 +test "$#" -gt $MAX_OPT && usage && exit 1 +while getopts "$OPTS" flag; do case "${flag}" in i) INPUT="${OPTARG}" @@ -66,13 +71,21 @@ while getopts "i:p" flag; do done # allow optional output filename -if [[ "$PRINT_OUT" == "true" && "$#" -eq 4 ]]; -then +if [[ "$#" -eq $MAX_OPT ]]; then OUTPUT="${@: -1}" -else +elif [[ + ("$PRINT_OUT" == "true") && + ( "$#" -eq 3) + ]]; then OUTPUT="${HOME}/av1_${INPUT}" +elif [[ "$#" -eq 2 ]]; then + OUTPUT="${HOME}/av1_${INPUT}" +else + OUTPUT="${@: -1}" fi +echo "INPUT: $INPUT, PRINT_OUT: $PRINT_OUT, OUTPUT: $OUTPUT" + encode # encode "$@" From 63b9b1622aa567c92ca8e542716541e89a8064b8 Mon Sep 17 00:00:00 2001 From: Levon Date: Thu, 18 Apr 2024 08:40:14 -0500 Subject: [PATCH 6/7] more echo --- scripts/recc_encode.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/recc_encode.sh b/scripts/recc_encode.sh index d8f2256..70322be 100755 --- a/scripts/recc_encode.sh +++ b/scripts/recc_encode.sh @@ -84,7 +84,9 @@ else OUTPUT="${@: -1}" fi +echo echo "INPUT: $INPUT, PRINT_OUT: $PRINT_OUT, OUTPUT: $OUTPUT" +echo encode From bfb95562d03112fba18cc76c2b79389d1cd3dd36 Mon Sep 17 00:00:00 2001 From: Levon Date: Fri, 19 Apr 2024 08:47:43 -0500 Subject: [PATCH 7/7] unmap --- scripts/recc_encode.sh | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/scripts/recc_encode.sh b/scripts/recc_encode.sh index 70322be..40ff717 100755 --- a/scripts/recc_encode.sh +++ b/scripts/recc_encode.sh @@ -11,40 +11,42 @@ usage() { } encode() { - echo ffmpeg -i \""$INPUT"\" -map 0 \ + ENCODE_FILE="/tmp/encode.sh" + + echo ffmpeg -i \""$INPUT"\" -map 0 $(unmap_streams "$INPUT") \ -af '"aformat=channel_layouts=7.1|5.1|stereo|mono"' -c:a libopus $(get_bitrate_audio "$INPUT") \ -c:s copy -c:V libsvtav1 -pix_fmt yuv420p10le -crf 25 -preset 3 -g 240 \ -svtav1-params \"tune=0:enable-overlays=1:scd=1:enable-hdr=1:fast-decode=1:enable-variance-boost=1\" \ - \""$OUTPUT"\" > /tmp/encode.sh + \""$OUTPUT"\" "&& mkvpropedit \"$OUTPUT\" --add-track-statistics-tags " > "$ENCODE_FILE" if [[ "$PRINT_OUT" == "true" ]]; then - cat /tmp/encode.sh + cat "$ENCODE_FILE" else - bash /tmp/encode.sh + bash "$ENCODE_FILE" fi } unmap_streams(){ INPUT="$1" - num_video_streams=$(ffprobe -v error -select_streams v -show_entries stream=index -of csv=p=0 "$INPUT" | wc -l) - for ((i = 0; i < num_video_streams; i++)); do - ffprobe -v error -select_streams "v:$i" -of default=noprint_wrappers=1:nokey=1 "$INPUT" - ffprobe -select_streams "v:0" -of default=noprint_wrappers=1:nokey=1 'first_20_DN.mkv' - ffprobe -v error -select_streams v -show_entries stream=index:stream_tags=type -of csv=p=0 'first_20_DN.mkv' + UNMAP_FILTER="jpeg|png" + UNMAP_STREAMS=$(ffprobe "$INPUT" 2>&1 | grep "Stream" | grep -Ei "$UNMAP_FILTER" | cut -d':' -f2 | tr '\n' ' ') + UNMAP_CMD="" + for UNMAP_STREAM in $UNMAP_STREAMS; do + UNMAP_CMD+="-map -0:$UNMAP_STREAM " done - echo "$num_video_streams" + echo "$UNMAP_CMD" } get_bitrate_audio() { - bitrate_cmd="" - num_audio_streams=$(ffprobe -v error -select_streams a -show_entries stream=index -of csv=p=0 "$INPUT" | wc -l) - for ((i = 0; i < num_audio_streams; i++)); do - num_channels=$(ffprobe -v error -select_streams "a:$i" -show_entries stream=channels -of default=noprint_wrappers=1:nokey=1 "$INPUT") - bitrate=$((num_channels * 64)) - bitrate_cmd+="-b:a:$i ${bitrate}k " + BITRATE_CMD="" + NUM_AUDIO_STREAMS=$(ffprobe -v error -select_streams a -show_entries stream=index -of csv=p=0 "$INPUT" | wc -l) + for ((i = 0; i < NUM_AUDIO_STREAMS; i++)); do + NUM_CHANNELS=$(ffprobe -v error -select_streams "a:$i" -show_entries stream=channels -of default=noprint_wrappers=1:nokey=1 "$INPUT") + BITRATE=$((NUM_CHANNELS * 64)) + BITRATE_CMD+="-b:a:$i ${BITRATE}k " done - echo "$bitrate_cmd" + echo "$BITRATE_CMD" } @@ -89,6 +91,3 @@ echo "INPUT: $INPUT, PRINT_OUT: $PRINT_OUT, OUTPUT: $OUTPUT" echo encode - -# encode "$@" -# unmap_streams "$@"