@echo off
setlocal EnableExtensions

title YTD Video Downloader

cd /d C:\YTD

if not exist "C:\YTD\yt-dlp.exe" (
    echo ERROR: C:\YTD\yt-dlp.exe was not found.
    echo.
    pause
    exit /b 1
)

if not exist "C:\YTD\FFMPEG\bin\ffmpeg.exe" (
    echo ERROR: FFmpeg was not found at:
    echo C:\YTD\FFMPEG\bin\ffmpeg.exe
    echo.
    pause
    exit /b 1
)

if not exist "C:\YTD\Videos" mkdir "C:\YTD\Videos"

echo.
set /p "URL=Enter video or playlist URL: "

if not defined URL (
    echo.
    echo No URL entered.
    pause
    exit /b 1
)

echo.
echo Downloading best available video and compatible audio...
echo.

"C:\YTD\yt-dlp.exe" ^
--ffmpeg-location "C:\YTD\FFMPEG\bin" ^
--windows-filenames ^
-f "bv*[ext=mp4]+ba[ext=m4a]/bv*+ba/b" ^
--merge-output-format mp4 ^
-o "C:\YTD\Videos\%%(title)s.%%(ext)s" ^
"%URL%"

set "RESULT=%ERRORLEVEL%"

echo.
if not "%RESULT%"=="0" (
    echo ========================================
    echo DOWNLOAD FAILED
    echo ========================================
    echo.
    pause
    exit /b %RESULT%
)

echo ========================================
echo DOWNLOAD COMPLETED SUCCESSFULLY
echo ========================================
echo.
echo Saved in C:\YTD\Videos
echo.
pause
exit /b 0
