@echo off
setlocal EnableExtensions EnableDelayedExpansion

if "%~2"=="" (
    echo Usage: %~nx0 image-file audio-file
    echo        %~nx0 audio-file image-file
    exit /b 1
)

call :classify "%~x1" kind1
call :classify "%~x2" kind2

if /I "!kind1!"=="image" if /I "!kind2!"=="audio" (
    set "image=%~f1"
    set "audio=%~f2"
    set "output=%~dpn2.mp4"
)

if /I "!kind1!"=="audio" if /I "!kind2!"=="image" (
    set "audio=%~f1"
    set "image=%~f2"
    set "output=%~dpn1.mp4"
)

if not defined audio (
    echo Expected one audio file and one image file.
    echo Received: "%~1" and "%~2"
    exit /b 1
)

ffmpeg -threads 3 -hwaccel auto -r 1 -loop 1 -i "!image!" -i "!audio!" -c:v libx264 -preset ultrafast -x264opts opencl -vf scale=1280:720 -tune stillimage -c:a copy -shortest "!output!"
exit /b %errorlevel%

:classify
set "%~2="
for %%I in (.jpg .jpeg .png .bmp .gif .webp) do if /I "%~1"=="%%~I" set "%~2=image"
for %%I in (.mp3 .wav .m4a .aac .flac .ogg .opus .wma .aif .aiff) do if /I "%~1"=="%%~I" set "%~2=audio"
exit /b 0
