Merge pull request #2 from Cosmic-Skye/fix-encoding-crashes-add-crf

Fix encoding script crashes and add CRF parameter support
This commit is contained in:
2025-06-16 17:43:42 -05:00
committed by GitHub
3 changed files with 29 additions and 12 deletions

View File

@@ -12,7 +12,7 @@ With the scripts, you can:
1. Install required dependencies using `./scripts/install_deps.sh` 1. Install required dependencies using `./scripts/install_deps.sh`
2. Build and install ffmpeg from source using `./scripts/build.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` 4. Install a film-grain estimation script using `./scripts/estimate_fg.sh -I`
5. Benchmark the different encoders using `./scripts/benchmark.sh` 5. Benchmark the different encoders using `./scripts/benchmark.sh`

View File

@@ -1,5 +1,8 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Enable extended pattern matching for ?() and +() operators
shopt -s extglob
usage() { usage() {
echo "./scripts/build.sh [options]" echo "./scripts/build.sh [options]"
echo -e "\th:\tdisplay this help output" echo -e "\th:\tdisplay this help output"
@@ -204,7 +207,7 @@ then
fi fi
# compilation job count # compilation job count
if commmand -v nproc 2> /dev/null ; then if command -v nproc 2> /dev/null ; then
THREADS="$(nproc)" THREADS="$(nproc)"
fi fi

View File

@@ -1,5 +1,8 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Enable extended pattern matching for ?() and +() operators
shopt -s extglob
# this is simply my recommended encoding method. # this is simply my recommended encoding method.
# do not take this as a holy grail. # do not take this as a holy grail.
@@ -16,6 +19,7 @@ usage() {
echo -e "\t[-v] Print relevant version info" echo -e "\t[-v] Print relevant version info"
echo -e "\t[-g NUM] set film grain for encode" echo -e "\t[-g NUM] set film grain for encode"
echo -e "\t[-P NUM] override default preset (3)" 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[output_file] if not set, will create at $HOME/"
echo -e "\n\t[-I] Install this as /usr/local/bin/encode" echo -e "\n\t[-I] Install this as /usr/local/bin/encode"
echo -e "\t[-U] Uninstall this from /usr/local/bin/encode" echo -e "\t[-U] Uninstall this from /usr/local/bin/encode"
@@ -23,7 +27,7 @@ usage() {
} }
get_duration() { 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() { get_crop() {
@@ -31,7 +35,7 @@ get_crop() {
local TOTAL_SECONDS="$(echo "$DURATION" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')" local TOTAL_SECONDS="$(echo "$DURATION" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')"
# get cropdetect value for first 1/5 of input # get cropdetect value for first 1/5 of input
local TIME_ENC="$(echo "$TOTAL_SECONDS / 2" | bc)" 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' \ -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=.* \ -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=.*" | sort -bh | uniq -c | sort -bh | tail -n1 | grep -o "crop=.*"
@@ -76,7 +80,7 @@ convert_subs() {
} }
ffmpeg_version() { 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() { video_enc_version() {
@@ -86,9 +90,9 @@ video_enc_version() {
audio_enc_version() { audio_enc_version() {
local AUDIO_ENC_VERSION local AUDIO_ENC_VERSION
if command -v ldd > /dev/null ; then 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 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 fi
local AUDIO_ENC_GIT="$(cd "$BUILDER_DIR/repos/opus" && git rev-parse --short HEAD)" 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}" test "$AUDIO_ENC_GIT" != '' && AUDIO_ENC_VERSION+="-g${AUDIO_ENC_GIT}"
@@ -124,7 +128,7 @@ encode() {
echo "export VIDEO_CROP=\"$VIDEO_CROP\"" >> "$ENCODE_FILE" echo "export VIDEO_CROP=\"$VIDEO_CROP\"" >> "$ENCODE_FILE"
fi 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" echo "export VIDEO_PARAMS=\"$VIDEO_PARAMS\"" >> "$ENCODE_FILE"
CONVERT_SUBS="$(convert_subs)" CONVERT_SUBS="$(convert_subs)"
@@ -148,7 +152,7 @@ encode() {
NL=' \\\n\t' NL=' \\\n\t'
echo >> "$ENCODE_FILE" echo >> "$ENCODE_FILE"
echo -e ffmpeg -i \""$INPUT"\" $NL \ echo -e /usr/local/bin/ffmpeg -i \""$INPUT"\" $NL \
-map 0 \$UNMAP \$VIDEO_CROP \ -map 0 \$UNMAP \$VIDEO_CROP \
\$AUDIO_FORMAT \$AUDIO_BITRATE $NL \ \$AUDIO_FORMAT \$AUDIO_BITRATE $NL \
-metadata \"\$FFMPEG_VERSION\" \ -metadata \"\$FFMPEG_VERSION\" \
@@ -157,7 +161,7 @@ encode() {
-metadata \"\$ADD_METADATA\" $NL \ -metadata \"\$ADD_METADATA\" $NL \
\$FFMPEG_PARAMS -dolbyvision 1 -svtav1-params \ \$FFMPEG_PARAMS -dolbyvision 1 -svtav1-params \
$NL \"\$SVT_PARAMS\" \"\$OUTPUT\" "||" $NL \ $NL \"\$SVT_PARAMS\" \"\$OUTPUT\" "||" $NL \
ffmpeg -i \""$INPUT"\" $NL \ /usr/local/bin/ffmpeg -i \""$INPUT"\" $NL \
-map 0 \$UNMAP \$VIDEO_CROP \ -map 0 \$UNMAP \$VIDEO_CROP \
\$AUDIO_FORMAT \$AUDIO_BITRATE $NL \ \$AUDIO_FORMAT \$AUDIO_BITRATE $NL \
-metadata \"\$FFMPEG_VERSION\" \ -metadata \"\$FFMPEG_VERSION\" \
@@ -181,7 +185,7 @@ encode() {
fi fi
} }
OPTS='vi:pcsg:P:IU' OPTS='vi:pcsg:P:C:IU'
NUM_OPTS="${#OPTS}" NUM_OPTS="${#OPTS}"
# default values # default values
CROP='false' CROP='false'
@@ -189,6 +193,7 @@ PRINT_OUT="false"
SAME_CONTAINER="false" SAME_CONTAINER="false"
GRAIN="" GRAIN=""
PRESET=3 PRESET=3
CRF=25
# only using -I/U # only using -I/U
MIN_OPT=1 MIN_OPT=1
# using all + output name # using all + output name
@@ -261,6 +266,15 @@ while getopts "$OPTS" flag; do
PRESET="${OPTARG}" PRESET="${OPTARG}"
OPTS_USED=$((OPTS_USED + 2)) 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" echo "wrong flags given"
usage usage
@@ -298,6 +312,6 @@ else
fi fi
echo 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 echo
encode encode