Open
Description
Idea
Hey,
I am trying to dynamically configure RF based on the source resolution. I have just found out different values are recommended for different resolutions:
I have 3 different folders for automated conversion:
MP4.0p.LOW
MP4.0p.MEDIUM
MP4.0p.HIGH
I was successfully able determine source resolution and calculate appropriate RF number. Now I struggle how to pass it to the handbrake automated conversion part so it overwrites value from the presets.json
.
My pre_conversion.sh
script:
#!/bin/sh
CONVERTED_FILE="$1"
SOURCE_FILE="$2"
PRESET="$3"
# Extract QUALITY from preset (last word after last dot)
QUALITY=$(echo "$PRESET" | awk -F. '{print toupper($NF)}')
echo "Converted file: $CONVERTED_FILE"
echo "Source file: $SOURCE_FILE"
echo "Preset: $PRESET"
echo "Quality extracted from preset: $QUALITY"
# Output scan for debugging
HandBrakeCLI -i "$SOURCE_FILE" --scan > /config/handbrake_scan_debug.txt 2>&1
# Try to extract the first size: line
size_line=$(grep -m 1 -E 'size: [0-9]+x[0-9]+' /config/handbrake_scan_debug.txt)
width=$(echo "$size_line" | sed -n 's/.*size: \([0-9]\+\)x\([0-9]\+\).*/\1/p')
height=$(echo "$size_line" | sed -n 's/.*size: \([0-9]\+\)x\([0-9]\+\).*/\2/p')
echo "Detected resolution: ${width}x${height}"
set_rf() {
local quality="$1"
local width="$2"
local height="$3"
if [ "$width" -le 1024 ] && [ "$height" -le 576 ]; then
# Standard Definition (SD): 480p/576p and below
case "$quality" in
LOW) echo 22 ;;
MEDIUM) echo 20 ;;
HIGH) echo 18 ;;
esac
elif [ "$width" -le 1280 ] && [ "$height" -le 720 ]; then
# High Definition (HD): 720p
case "$quality" in
LOW) echo 23 ;;
MEDIUM) echo 21 ;;
HIGH) echo 19 ;;
esac
elif [ "$width" -le 1920 ] && [ "$height" -le 1080 ]; then
# Full High Definition (Full HD): 1080p
case "$quality" in
LOW) echo 24 ;;
MEDIUM) echo 22 ;;
HIGH) echo 20 ;;
esac
else
# Ultra High Definition (UHD): 2160p (4K) and above
case "$quality" in
LOW) echo 28 ;;
MEDIUM) echo 25 ;;
HIGH) echo 22 ;;
esac
fi
}
RF=$(set_rf "$QUALITY" "$width" "$height")
export HANDBRAKE_CLI_OPTIONS="-q $RF"
echo "Set RF to $RF (quality: $QUALITY, resolution: ${width}x${height})"
So probably the easiest solution would be to implement an environment variable (e.g. HANDBRAKE_CLI_OPTIONS
) which would be used at the end of conversion startup script - thus overwriting RF value from the presets.json
. Thanks