pick_rez_quality.sh

Browser view only. Use the download link if you want to save the file.

Back to Toolbox
#!/bin/bash

# Arrays for settings
resolutions=("noscale" "1080p" "720p" "480p")
rezstrings=("" "-s 1920:1080" "-s 1280:720" "-s 720:480")
qualities=("sameq" "crf18" "crf20" "crf22" "crf24")
qparams=("-q:v 0" "-crf 18" "-crf 20" "-crf 22" "-crf 24")
aspects=("Match source" "16x9" "4x3" "TopBotCrop")
asparams=("" "-aspect 16:9" "4x3" "TopBotCrop")

# Initialize selected indices
selected_resolution_index=0
selected_quality_index=0
selected_aspect_index=0

# if preset_rez is set, skip questions
if [[ -v preset_mode && -v preset_rez ]]; then
    
    #hardcode sameq for now
    selected_quality_index=0

    # preset the indexes and just use copy of code from below
    if [[ $preset_rez == "720p" ]]; then
        selected_resolution_index=2
    elif [[ $preset_rez == "1080p" ]]; then
        selected_resolution_index=1
    elif [[ $preset_rez == "noscale" ]]; then
        selected_resolution_index=0
    fi    

    # similar for aspect
    if [[ $preset_aspect == "16x9" ]]; then
        selected_aspect_index=1
    elif [[ $preset_aspect == "" ]]; then
        selected_aspect_index=0
    elif [[ $preset_aspect == "4x3" ]]; then
        selected_aspect_index=2
    elif [[ $preset_aspect == "TopBotCrop" ]]; then
        selected_aspect_index=3
    fi    

    # similar for quality
    if [[ $preset_quality == "same" ]]; then
        selected_quality_index=0
    elif [[ $preset_quality == "sameq" ]]; then
        selected_quality_index=0
    elif [[ $preset_quality == "" ]]; then
        selected_aspect_index=0
    elif [[ $preset_quality == "crf18" ]]; then
        selected_quality_index=1
    elif [[ $preset_quality == "crf20" ]]; then
        selected_quality_index=2
    elif [[ $preset_quality == "crf22" ]]; then
        selected_quality_index=3
    elif [[ $preset_quality == "crf24" ]]; then
        selected_quality_index=4
    fi    

    # Set scale and qstrings based on selected indices
    resolution="${resolutions[$selected_resolution_index]}"
    scale="${rezstrings[$selected_resolution_index]}"
    quality="${qualities[$selected_quality_index]}"
    qparam="${qparams[$selected_quality_index]}"
    aspect="${aspects[$selected_aspect_index]}"         #ext additions
    aspectstring="${asparams[$selected_aspect_index]}"  #actual ffmpeg params

    # Display final settings
    echo
    echo "You have selected:"
    echo "Resolution = $resolution ($scale)"
    echo "Quality: $quality ($qparam)"
    echo "Aspect: $aspect ($aspectstring)"
    echo
    return 1  # got the values, GTFO
fi


###  No preset used, drop into normal display
# Function to display the current selections
display_menu() {
    clear
    echo
    echo "Select your settings:"
    echo

    # Display resolutions horizontally
    echo -e -n "[\e[1;31mR\e[0m] Resolutions: "
    for i in "${!resolutions[@]}"; do
        if [ "$i" -eq "$selected_resolution_index" ]; then
            echo -e -n "\e[1;97;41m${resolutions[$i]}\e[0m  "  # Bright white on red for selected
        else
            echo -n "${resolutions[$i]}  "  # Not selected
        fi
    done

    echo  # New line
    echo  # New line

    # Display qualities horizontally
    echo -e -n "[\e[1;31mQ\e[0m] Qualities: "
    for i in "${!qualities[@]}"; do
        if [ "$i" -eq "$selected_quality_index" ]; then
            echo -e -n "\e[1;97;41m${qualities[$i]}\e[0m  "  # Bright white on red for selected
        else
            echo -n "${qualities[$i]}  "  # Not selected
        fi
    done

    echo  # New line
    echo  # New line

    # Display aspects horizontally
    echo -e -n "[\e[1;31mA\e[0m] Aspect Ratios: "
    for i in "${!aspects[@]}"; do
        if [ "$i" -eq "$selected_aspect_index" ]; then
            echo -e -n "\e[1;97;41m${aspects[$i]}\e[0m  "  # Bright white on red for selected
        else
            echo -n "${aspects[$i]}  "  # Not selected
        fi
    done

    echo
    echo
}

# Function to read a single keypress with proper ESC handling
read_keypress() {
    # Read the first character
    IFS= read -rsn1 key 2>/dev/null
    if [[ $key == $'\e' ]]; then
        # Attempt to read two more characters with a very short timeout
        read -rsn2 -t 0.0001 key_extra 2>/dev/null
        key+="$key_extra"
    fi
    echo "$key"
}

# Main loop for input
while true; do
    display_menu
    echo
    echo "Press R, Q, or A to change settings, ENTER to launch, ESC to quit"

    # Prompt for user input
    key=$(read_keypress)

    case "$key" in
        [Rr])  # Select resolution
            ((selected_resolution_index++))
            if [ "$selected_resolution_index" -ge "${#resolutions[@]}" ]; then
                selected_resolution_index=0  # Wrap around
            fi
            ;;
        [Qq])  # Select quality
            ((selected_quality_index++))
            if [ "$selected_quality_index" -ge "${#qualities[@]}" ]; then
                selected_quality_index=0  # Wrap around
            fi
            ;;
        [Aa])  # Select aspect
            ((selected_aspect_index++))
            if [ "$selected_aspect_index" -ge "${#aspects[@]}" ]; then
                selected_aspect_index=0  # Wrap around
            fi
            ;;
        $'\e')  # ESC key to quit
            echo "Exiting settings."
            exit 0
            ;;
        "")  # Enter key to accept and go
            break  # Exit the loop to proceed
            ;;
        $'\t')  # TAB key pressed
            # Do nothing; ignore TAB key
            ;;
        *)  # Any other key
            echo "Invalid key pressed. Please use R, Q, A, ENTER, or ESC."
            sleep 1
            ;;
    esac
done

# Set scale and qstrings based on selected indices
resolution="${resolutions[$selected_resolution_index]}"
scale="${rezstrings[$selected_resolution_index]}"
quality="${qualities[$selected_quality_index]}"
qparam="${qparams[$selected_quality_index]}"
aspect="${aspects[$selected_aspect_index]}"         #ext additions
aspectstring="${asparams[$selected_aspect_index]}"  #actual ffmpeg params

## Match Source was a placeholder for no param
if [[ "$aspect" == "Match source" ]]; then
    aspect=""
fi

# Display final settings
echo
echo "You have selected:"
echo "Resolution = $resolution ($scale)"
echo "Quality: $quality ($qparam)"
echo "Aspect: $aspect ($aspectstring)"
echo

echo "Waiting 1 sec..."
sleep 1

# You can add your launch command here, for example:
# ffmpeg [your parameters based on selections]