@echo off
setlocal enabledelayedexpansion

:: Resolve the folder where this .bat lives
set "DIR=%~dp0"
set "FFMPEG=%DIR%ffmpeg.exe"

:: Check ffmpeg exists
if not exist "%FFMPEG%" (
    echo [ERROR] ffmpeg.exe not found in the same folder as this script.
    pause
    exit /b 1
)

set "FOUND=0"

:: Loop through mp4 and webm files
for %%E in (mp4 webm) do (
    for %%F in ("%DIR%*.%%E") do (
        set "FOUND=1"
        set "INPUT=%%~fF"
        set "OUTPUT=%%~dpnF.jpg"

        echo Extracting first frame from: %%~nxF
        "%FFMPEG%" -i "!INPUT!" -frames:v 1 "!OUTPUT!" -y 2>nul

        if exist "!OUTPUT!" (
            echo Done: %%~nF.jpg
        ) else (
            echo [ERROR] Failed to extract frame from %%~nxF
        )
    )
)

if "!FOUND!"=="0" (
    echo No mp4 or webm files found in this folder.
)

echo.
echo All done!
pause