pick_conversions.sh

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

Back to Toolbox
#!/usr/bin/env bash

##############################################################################
# pick_conversions.sh
# 
# This script displays a list of video files (in the array `videofiles[@]`),
# allows the user to remove (delete) files from conversion by toggling them off,
# and finally builds an array `filenames[]` of the remaining files. Then (if enabled)
# it calculates the needed dB boost to reach near 0 dB for each file and populates `gains[]`.
##############################################################################

# ---------------------------------------------------------------------------
# 1) CONFIG & DECLARATIONS
# ---------------------------------------------------------------------------

update_title() {
    local message="$1"
    echo -ne "\033]0;$message\007"
}

# Declare a parallel array for dB gains
declare -a gains=()

# Initialize arrays
file_base_arr=()
outfiles=()
durations=()  # Array to store durations
declare -A file_status  # Associative array to track the status of each file (enabled/disabled)
filenames=()  # Array to store selected files (initially empty)

# Assume videofiles is already populated and passed in from previous code
# Also assume $MODE and $targetEXT are set outside this script

# Set curve options
curvenum=0
curve=""
curve1="-vf 'curves=preset=lighter'"
curve2="-vf 'eq=gamma=1.5:saturation=1.3,unsharp=5:5:2'"
curve3="-vf 'curves=all='0/0 0.5/1 1/1''"

if [ "$curvenum" -eq 0 ]; then
    echo "No curve applied"
else
    case "$curvenum" in
        1) curve="$curve1";;
        2) curve="$curve2";;
        3) curve="$curve3";;
    esac
    echo "Curve is on"
    sleep 5
fi

# ---------------------------------------------------------------------------
# 2) INTERACTION LOOP TO TOGGLE (REMOVE) FILES
# ---------------------------------------------------------------------------

# ✅ NEW: Auto-select single video without prompt
if [[ ${#videofiles[@]} -eq 1 ]]; then
    echo
    echo "Single video detected: ${videofiles[0]##*/}"
    echo "Automatically selecting it for conversion..."
    filenames=("${videofiles[0]}")

    echo
    echo "Selected files for conversion:"
    echo "${filenames[0]#./}"

    echo
    echo "Press a key or wait 2 seconds..."
    read -rsn1 -t 2 input
    if [[ $input == 'q' || $input == 'Q' ]]; then
        echo "Exiting the script."
        kill -- "$PPID"
        exit 0
    fi
else
    # --- existing interactive toggle loop ---
    while true; do
        clear
        echo

        # Determine color based on MODE in uppercase
        case "$MODE" in
            MKV) color="\033[41;97m" ;;  # Red background, white text
            MP4) color="\033[42;97m" ;;  # Green background, white text
            MPG|MPEG1) color="\033[44;97m" ;;  # Blue background, white text
            MP3) color="\033[45;97m" ;;  # Magenta background, white text
            *) color="\033[47;30m" ;;    # Default: White background, black text
        esac

        # Output with the dynamic color
        script_name=$(basename "$0")
        if [[ "$script_name" == "concat.sh" ]]; then
            echo -e "Welcome to $script_name. Making ${color} CONCATTED \033[0m files."
        elif [[ -n "$MODE" ]]; then
            echo -e "Welcome to $script_name. Making ${color} $MODE \033[0m files."
        fi

        echo
        echo "VIDEOS FOUND:"
        
        # Display file list with statuses
        for i in "${!videofiles[@]}"; do
            if [ -n "${videofiles[$i]}" ]; then
                file_base=$(basename "${videofiles[$i]##./}" | sed 's/\.[^.]*$//')

                duration_seconds=$(ffprobe -v error -show_entries format=duration \
                    -of default=noprint_wrappers=1:nokey=1 "${videofiles[$i]}")

                if [[ $duration_seconds =~ ^[0-9]*\.?[0-9]+$ ]]; then
                    duration_formatted=$(printf "%02d:%02d:%02d" \
                        $(echo "$duration_seconds" | awk '{print int($1/3600), int(($1%3600)/60), int($1%60)}'))
                else
                    duration_formatted="00:00:00"
                fi

                outfile="${file_base}.curve${curvenum}.$targetEXT"
                if [ "$curvenum" -eq 0 ]; then
                    outfile="${file_base}.$targetEXT"
                fi

                if [[ -z "${file_base_arr[$i]}" ]]; then
                    file_base_arr+=("$file_base")
                    outfiles+=("$outfile")
                    durations+=("$duration_formatted")
                    file_status[$i]=1
                fi

                echo -e "\e[1;31m[$((i+1))] \"${videofiles[$i]##*/}\" -> \"$outfile\" (${durations[$i]})\e[0m"
            fi
        done

        echo
        echo "Press a number (1-${#file_base_arr[@]}) to remove that file from conversion,"
        echo "ENTER to start conversions, or 'q' / ESC to quit:"
        
        read -rsn1 input

        if [[ $input =~ ^[1-9]$ && $input -le ${#file_base_arr[@]} ]]; then
            index=$((input - 1))
            unset videofiles[$index] file_base_arr[$index] outfiles[$index] durations[$index] file_status[$index]
            videofiles=("${videofiles[@]}")
            file_base_arr=("${file_base_arr[@]}")
            outfiles=("${outfiles[@]}")
            durations=("${durations[@]}")
            new_status=()
            for i in "${!file_base_arr[@]}"; do new_status[i]=1; done
            file_status=("${new_status[@]}")

        elif [[ $input == 'G' || $input == 'g' || $input == '' ]]; then
            filenames=()
            for i in "${!videofiles[@]}"; do
                filenames+=("${videofiles[$i]}")
            done

            echo
            echo "Selected files for conversion:"
            for file in "${filenames[@]}"; do
                echo "${file#./}"
            done

            echo
            echo "Press a key or wait 3 seconds..."
            read -rsn1 -t 3 input
            if [[ $input == 'q' || $input == 'Q' ]]; then
                echo "Exiting the script."
                kill -- "$PPID"
                exit 0
            fi
            break
        elif [[ $input == 'q' || $input == 'Q' || $input == $'\033' ]]; then
            echo "Exiting the script."
            kill -- "$PPID"
            exit 0
        fi
    done
fi

# End of pick_conversions.sh