1
#!/bin/bash
# Get the directory where the script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Function to list and select video files
select_input_video() {
# Find video files in the script's directory
readarray -d '' VIDEO_FILES < <(find "$SCRIPT_DIR" -maxdepth 1 -type f \( -name "*.mp4" -o -name "*.mkv" -o -name "*.avi" \) -print0 | sort -z)
# Check if any video files found
if [ ${#VIDEO_FILES[@]} -eq 0 ]; then
echo "No video files found in the script's directory."
exit 1
fi
# Display ALL video files with numbered menu
echo "Found Video Files:"
for i in "${!VIDEO_FILES[@]}"; do
echo "$((i+1)). $(basename "${VIDEO_FILES[i]}")"
done
# Select video file
while true; do
read -p "Enter the number of the video file to upscale (or 'q' to quit): " VIDEO_CHOICE
# Allow quitting
if [[ "$VIDEO_CHOICE" == "q" ]]; then
echo "Exiting script."
exit 0
fi
if [[ "$VIDEO_CHOICE" =~ ^[0-9]+$ ]] &&
[ "$VIDEO_CHOICE" -gt 0 ] &&
[ "$VIDEO_CHOICE" -le ${#VIDEO_FILES[@]} ]; then
INPUT_VIDEO="${VIDEO_FILES[$((VIDEO_CHOICE-1))]}"
break
else
echo "Invalid selection. Please try again."
fi
done
}
# Function to choose upscaler
choose_upscaler() {
echo "Choose Upscaler:"
echo "--- For Anime / Cartoons ---"
echo "1. RealCUGAN (Anime)"
echo "2. RealESRGAN (Anime Video v3)"
echo "--- For General / Live-Action ---"
echo "3. RealESRGAN (General/Live-Action) - high quality (realesrgan-plus)"
while true; do
read -p "Enter 1, 2, or 3: " choice # Updated prompt for 3 options
case $choice in
1)
UPSCALER="realcugan"
REALESRGAN_MODEL_ARGS=() # Empty array for RealCUGAN
break
;;
2)
UPSCALER="realesrgan"
REALESRGAN_MODEL_ARGS=("--realesrgan-model" "realesr-animevideov3")
break
;;
3)
UPSCALER="realesrgan"
# Corrected model name based on --help output
REALESRGAN_MODEL_ARGS=("--realesrgan-model" "realesrgan-plus")
# REMINDER: For this model to work, you need 'RealESRGAN_x4plus.pth'
# downloaded from https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth
# and placed in '~/.config/video2x/models/RealESRGAN/' or within the AppImage's models directory.
break
;;
*)
echo "Invalid choice. Please enter 1, 2, or 3." # Updated prompt
;;
esac
done
}
# Function to choose encoder (now only CPU option)
choose_encoder() {
echo "Output Video Encoder: CPU (libx264) will be used."
VIDEO_CODEC="libx264" # Default and only option
}
# Function to get upscale factor - NOW CONDITIONAL ON UPSCALER
get_upscale_factor() {
local valid_scales
local prompt_message
if [[ "$UPSCALER" == "realesrgan" && "${REALESRGAN_MODEL_ARGS[1]}" == "realesrgan-plus" ]]; then
valid_scales=(4)
prompt_message="Enter upscale factor (only 4x is supported for realesrgan-plus): "
else
valid_scales=(2 3 4)
prompt_message="Enter upscale factor (2, 3, or 4): "
fi
while true; do
read -p "$prompt_message" SCALE
local is_valid=0
for s in "${valid_scales[@]}"; do
if [[ "$SCALE" == "$s" ]]; then
is_valid=1
break
fi
done
if [[ "$is_valid" -eq 1 ]]; then
break
else
echo "Invalid upscale factor. Please choose one of the supported factors: ${valid_scales[*]}."
fi
done
}
# Function to get quality setting
get_quality_setting() {
echo "Choose Output Quality (Lower value = Higher Quality/Larger File):"
echo "This is CRF (Constant Rate Factor) for libx264: 18-24 is a good range."
while true; do
read -p "Enter a quality value (e.g., 21): " QUALITY_VALUE
if [[ "$QUALITY_VALUE" =~ ^[0-9]+$ ]] && [ "$QUALITY_VALUE" -ge 0 ] && [ "$QUALITY_VALUE" -le 51 ]; then
break
else
echo "Invalid quality value. Please enter a number between 0 and 51."
fi
done
}
# Main script
main() {
clear
echo "Video Upscaling Script"
echo "--------------------"
# Find and select input video
select_input_video
# Choose upscaler and model
choose_upscaler
# Choose encoder (now fixed to CPU)
choose_encoder
# Get upscale factor (now conditional)
get_upscale_factor
# Get quality setting
get_quality_setting
# Generate output filename
FILENAME=$(basename "$INPUT_VIDEO")
FILENAME_NOEXT="${FILENAME%.*}"
OUTPUT_VIDEO="${SCRIPT_DIR}/${FILENAME_NOEXT}_upscaled.mp4"
# Common options for video2x
COMMON_ARGS=("-i" "$INPUT_VIDEO" "-o" "$OUTPUT_VIDEO" "-s" "$SCALE")
# Encoder specific options (now only for libx264)
ENCODER_ARGS=("-c" "$VIDEO_CODEC" "-e" "crf=$QUALITY_VALUE" "-e" "preset=medium")
# Construct the final command based on upscaler choice
if [[ "$UPSCALER" == "realesrgan" ]]; then
CMD=(video2x "${COMMON_ARGS[@]}" -p realesrgan "${REALESRGAN_MODEL_ARGS[@]}" "${ENCODER_ARGS[@]}")
else # RealCUGAN
CMD=(video2x "${COMMON_ARGS[@]}" -p realcugan "${ENCODER_ARGS[@]}")
fi
# Execute the command
echo -e "\nStarting upscaling..."
echo "Upscaler: ${UPSCALER^^}"
if [[ "$UPSCALER" == "realesrgan" && ${#REALESRGAN_MODEL_ARGS[@]} -gt 0 ]]; then
echo "RealESRGAN Model: ${REALESRGAN_MODEL_ARGS[1]}" # Display the model name
fi
echo "Output Codec: $VIDEO_CODEC (CPU)"
echo "CRF Quality: $QUALITY_VALUE"
echo "Input: $INPUT_VIDEO"
echo "Output: $OUTPUT_VIDEO"
echo "Scale: ${SCALE}x"
echo -e "\nExecuting command: ${CMD[@]}\n" # Show the full command being run
"${CMD[@]}"
# Check command status
if [ $? -eq 0 ]; then
echo -e "\nUpscaling completed successfully! Output file: $OUTPUT_VIDEO"
else
echo -e "\nError: Upscaling failed. Please check the video2x output above for details."
fi
}
# Call the main function to start the script
main
For immediate assistance, please email our customer support: [email protected]