diff --git a/README.md b/README.md index 4abe1c9..ba12cb5 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ With the scripts, you can: 1. Install required dependencies using `./scripts/install_deps.sh` 2. Build and install ffmpeg from source using `./scripts/build.sh` -3. Install an encoding script using `./scripts/recc_encode_install.sh -I` +3. Install an encoding script using `./scripts/recc_encode.sh -I` 4. Install a film-grain estimation script using `./scripts/estimate_fg.sh -I` 5. Benchmark the different encoders using `./scripts/benchmark.sh` diff --git a/scripts/build.sh b/scripts/build.sh index 3793288..3544da4 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,5 +1,8 @@ #!/usr/bin/env bash +# Enable extended pattern matching for ?() and +() operators +shopt -s extglob + usage() { echo "./scripts/build.sh [options]" echo -e "\th:\tdisplay this help output" @@ -204,7 +207,7 @@ then fi # compilation job count -if commmand -v nproc 2> /dev/null ; then +if command -v nproc 2> /dev/null ; then THREADS="$(nproc)" fi diff --git a/scripts/recc_encode.sh b/scripts/recc_encode.sh index 63cd3b7..4de6231 100755 --- a/scripts/recc_encode.sh +++ b/scripts/recc_encode.sh @@ -1,5 +1,8 @@ #!/usr/bin/env bash +# Enable extended pattern matching for ?() and +() operators +shopt -s extglob + # this is simply my recommended encoding method. # do not take this as a holy grail. @@ -16,6 +19,7 @@ usage() { echo -e "\t[-v] Print relevant version info" echo -e "\t[-g NUM] set film grain for encode" echo -e "\t[-P NUM] override default preset (3)" + echo -e "\t[-C NUM] override default CRF (25)" echo -e "\n\t[output_file] if not set, will create at $HOME/" echo -e "\n\t[-I] Install this as /usr/local/bin/encode" echo -e "\t[-U] Uninstall this from /usr/local/bin/encode" @@ -23,7 +27,7 @@ usage() { } get_duration() { - ffmpeg -i "$1" 2>&1 | grep "Duration" | awk '{print $2}' | tr -d , + /usr/local/bin/ffmpeg -i "$1" 2>&1 | grep "Duration" | awk '{print $2}' | tr -d , } get_crop() { @@ -31,7 +35,7 @@ get_crop() { local TOTAL_SECONDS="$(echo "$DURATION" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')" # get cropdetect value for first 1/5 of input local TIME_ENC="$(echo "$TOTAL_SECONDS / 2" | bc)" - ffmpeg -hide_banner -ss 0 -discard 'nokey' -i "$INPUT" -t "$TIME_ENC" \ + /usr/local/bin/ffmpeg -hide_banner -ss 0 -discard 'nokey' -i "$INPUT" -t "$TIME_ENC" \ -map '0:v:0' -filter:v:0 'cropdetect=limit=100:round=16:skip=2:reset_count=0' \ -codec:v 'wrapped_avframe' -f 'null' '/dev/null' -y 2>&1 | grep -o crop=.* \ | sort -bh | uniq -c | sort -bh | tail -n1 | grep -o "crop=.*" @@ -76,7 +80,7 @@ convert_subs() { } ffmpeg_version() { - ffmpeg -version 2>&1 | head -n 1 | grep version | cut -d' ' -f1-3 + /usr/local/bin/ffmpeg -version 2>&1 | head -n 1 | grep version | cut -d' ' -f1-3 } video_enc_version() { @@ -86,9 +90,9 @@ video_enc_version() { audio_enc_version() { local AUDIO_ENC_VERSION if command -v ldd > /dev/null ; then - AUDIO_ENC_VERSION="$(ldd "$(which ffmpeg)" | grep -i libopus | cut -d' ' -f3 | xargs readlink)" + AUDIO_ENC_VERSION="$(ldd "/usr/local/bin/ffmpeg" | grep -i libopus | cut -d' ' -f3 | xargs readlink)" elif command -v otool > /dev/null ; then - AUDIO_ENC_VERSION="$(otool -L $(which ffmpeg) | grep libopus | tr -d ')' | awk -F' ' '{print $NF}')" + AUDIO_ENC_VERSION="$(otool -L /usr/local/bin/ffmpeg | grep libopus | tr -d ')' | awk -F' ' '{print $NF}')" fi local AUDIO_ENC_GIT="$(cd "$BUILDER_DIR/repos/opus" && git rev-parse --short HEAD)" test "$AUDIO_ENC_GIT" != '' && AUDIO_ENC_VERSION+="-g${AUDIO_ENC_GIT}" @@ -124,7 +128,7 @@ encode() { echo "export VIDEO_CROP=\"$VIDEO_CROP\"" >> "$ENCODE_FILE" fi - VIDEO_PARAMS="-pix_fmt yuv420p10le -crf 25 -preset $PRESET -g 240" + VIDEO_PARAMS="-pix_fmt yuv420p10le -crf $CRF -preset $PRESET -g 240" echo "export VIDEO_PARAMS=\"$VIDEO_PARAMS\"" >> "$ENCODE_FILE" CONVERT_SUBS="$(convert_subs)" @@ -148,7 +152,7 @@ encode() { NL=' \\\n\t' echo >> "$ENCODE_FILE" - echo -e ffmpeg -i \""$INPUT"\" $NL \ + echo -e /usr/local/bin/ffmpeg -i \""$INPUT"\" $NL \ -map 0 \$UNMAP \$VIDEO_CROP \ \$AUDIO_FORMAT \$AUDIO_BITRATE $NL \ -metadata \"\$FFMPEG_VERSION\" \ @@ -157,7 +161,7 @@ encode() { -metadata \"\$ADD_METADATA\" $NL \ \$FFMPEG_PARAMS -dolbyvision 1 -svtav1-params \ $NL \"\$SVT_PARAMS\" \"\$OUTPUT\" "||" $NL \ - ffmpeg -i \""$INPUT"\" $NL \ + /usr/local/bin/ffmpeg -i \""$INPUT"\" $NL \ -map 0 \$UNMAP \$VIDEO_CROP \ \$AUDIO_FORMAT \$AUDIO_BITRATE $NL \ -metadata \"\$FFMPEG_VERSION\" \ @@ -181,7 +185,7 @@ encode() { fi } -OPTS='vi:pcsg:P:IU' +OPTS='vi:pcsg:P:C:IU' NUM_OPTS="${#OPTS}" # default values CROP='false' @@ -189,6 +193,7 @@ PRINT_OUT="false" SAME_CONTAINER="false" GRAIN="" PRESET=3 +CRF=25 # only using -I/U MIN_OPT=1 # using all + output name @@ -261,6 +266,15 @@ while getopts "$OPTS" flag; do PRESET="${OPTARG}" OPTS_USED=$((OPTS_USED + 2)) ;; + C) + if [[ ${OPTARG} != ?(-)+([[:digit:]]) || ${OPTARG} -lt 0 || ${OPTARG} -gt 63 ]]; then + echo "${OPTARG} is not a valid CRF value (0-63)" + usage + exit 1 + fi + CRF="${OPTARG}" + OPTS_USED=$((OPTS_USED + 2)) + ;; *) echo "wrong flags given" usage @@ -298,6 +312,6 @@ else fi echo -echo "INPUT: $INPUT, PRINT_OUT: $PRINT_OUT, GRAIN: $GRAIN, OUTPUT: $OUTPUT" +echo "INPUT: $INPUT, PRINT_OUT: $PRINT_OUT, GRAIN: $GRAIN, PRESET: $PRESET, CRF: $CRF, OUTPUT: $OUTPUT" echo encode