#!/bin/bash

# Directory to scan for video files (default: current directory)
DIR=${1:-.}

# Supported video file extensions
EXTENSIONS=("mp4" "mkv" "avi" "mov" "wmv" "flv" "webm" "MP4" "MKV" "AVI" "MOV" "WMV" "FLV" "WEBM")

# Function to test a video file with ffmpeg
test_video() {
    local file="$1"
    echo "Testing video: $file"
    # Run ffmpeg in error-detection mode
    ffmpeg -v error -i "$file" -f null - 2> >(grep -E '.*' || true)
}

# Find and test video files
if [[ ! -d "$DIR" ]]; then
    echo "Error: Directory '$DIR' does not exist."
    exit 1
fi

/home/honney/.bin/tracker.py add ff_testing

echo "Scanning directory: $DIR"
for ext in "${EXTENSIONS[@]}"; do
    # Find files with the current extension
    find "$DIR" -type f -iname "*.$ext" | while IFS= read -r file; do
        # Quote the filename to handle spaces correctly
        ERRORS=$(test_video "$file")
        if [[ -n "$ERRORS" ]]; then
            echo -e "\nErrors found in: $file"
            echo "$ERRORS"
        fi
    done
done

echo -e "\nScan complete."
