load_video_files.sh

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

Back to Toolbox
# Detect video files in the current directory and sort them alphabetically
# (old way, ok for years) IFS=$'\n' read -rd '' -a videofiles <<< "$(find . -maxdepth 1 -type f \( -iname "*.mkv" -o -iname "*.mp4" -o -iname "*.mov" -o -iname "*.m2ts" -o -iname "*.mp3" \) | sort)"
mapfile -t videofiles < <(find . -maxdepth 1 -type f \( -iname "*.mkv" -o -iname "*.mp4" -o -iname "*.mov" -o -iname "*.m2ts" -o -iname "*.mp3" -o -iname "*.mts" \) | sort)

SHOW_RAW_FILES_FOUND=0

# Initialize separate arrays for each video type
mp4s=()
mkvs=()
movs=()
mtss=()
mp3s=()

# Populate the respective arrays based on the file extension
for file in "${videofiles[@]}"; do
    case "${file,,}" in  # Convert to lowercase for case-insensitivity
        *.mkv)
            mkvs+=("$file")
            ;;
        *.mp4)
            mp4s+=("$file")
            ;;
        *.mov)
            movs+=("$file")
            ;;
        *.mts)
            mtss+=("$file")
            ;;
        *.m2ts)
            m2ts+=("$file")
            ;;
        *.mp3)
            mp3s+=("$file")
            ;;
    esac
done

# Check if any video files were found
if [ ${#videofiles[@]} -eq 0 ]; then
    echo "No video files (.mkv, .mp4, .mov, .mts) found in the current directory."
    echo "Window closing in 5 seconds..."
    sleep 5
    kill -- "$PPID"
    exit
fi

if [[ ${SHOW_RAW_FILES_FOUND:-0} -eq 1 ]]; then
    # Example: Display the arrays (optional)
    if [ ${#mkvs[@]} -gt 0 ]; then
        echo "VIDEOS FOUND: ${mkvs[@]}"
    fi

    if [ ${#mp4s[@]} -gt 0 ]; then
        echo "VIDEOS FOUND: ${mp4s[@]}"
    fi

    if [ ${#movs[@]} -gt 0 ]; then
        echo "VIDEOS FOUND: ${movs[@]}"
    fi

    if [ ${#mtss[@]} -gt 0 ]; then
        echo "VIDEOS FOUND: ${mtss[@]}"
    fi

    if [ ${#m2ts[@]} -gt 0 ]; then
        echo "VIDEOS FOUND: ${m2ts[@]}"
    fi

    if [ ${#mp3s[@]} -gt 0 ]; then
        echo "AUDIOS FOUND: ${mp3s[@]}"
    fi

    sleep 4
fi