Skip to content

Instantly share code, notes, and snippets.

@C0nw0nk
Created November 10, 2022 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save C0nw0nk/e3c7d48f74fbc0268d72430543ac7184 to your computer and use it in GitHub Desktop.
Save C0nw0nk/e3c7d48f74fbc0268d72430543ac7184 to your computer and use it in GitHub Desktop.
youtube-dl.exe youtube-dl.cmd ffmpeg.exe batch file script.
:: youtube-dl.cmd [url[.txt]] [...]
:: download youtube-dl.exe from https://github.com/ytdl-org/youtube-dl/releases/latest/download/youtube-dl.exe
:: download ffmpeg.exe from https://github.com/C0nw0nk/youtube-dl-concurrent/raw/main/ffmpeg.exe
:: any combination of URLs and text files may be used.
:: text files are found and processed first, then all URLs.
@ECHO OFF
:: set target folder, else current folder is used
SET TargetFolder=D:\Downloads\YouTube
:: if defined will create folders for each text list entered
SET MakeListDir=Y
:: set youtube-dl.exe commandline options, all options MUST begin with a space
:: name shorthand: %~d0=D:,%~p0=\path\to\,%~n0=name,%~x0=.ext,%~f0=%~dpnx0
:: remember to double percentage signs when used as output in batch files
SET OutTemplate= -o "%%(playlist)s\%%(title)s.%%(ext)s"
SET Options= --ignore-errors
SET Network=
SET GeoRestrict=
SET VideoSelect= --download-archive "%~f0.archive"
SET Download= --limit-rate 1024K
SET FileSystem=
SET Thumbnail=
SET Verbosity= --console-title
SET WorkArounds=
SET VideoFormat= --format "bestvideo[vbr<2097152]+bestaudio/bestvideo[height<=720]+bestaudio/bestvideo+bestaudio/best"
SET Subtitle= --sub-lang eng
SET Authenticate=
SET AdobePass=
SET PostProcess= --embed-subs --add-metadata
:: set terminal size and codepage to unicode (65001=UTF-8)
MODE 80,10000
CHCP 65001 >NUL
:: capture commandline, add batch folder to path, create and enter target
SET source=%*
CALL SET "PATH=%~dp0;%%PATH:%~dp0;=%%"
MD "%TargetFolder%" >NUL 2>&1
PUSHD "%TargetFolder%"
:getURL -- main loop
:: prompt for source, exit if empty, call routines, loop
IF NOT DEFINED source SET /P "source=Enter url[.txt]: "
IF NOT DEFINED source POPD & EXIT /B %appErr%
ECHO.
CALL :getLST %source%
CALL :doYTDL %source%
SET source=
GOTO :getURL
:getLST url[.txt] [...]
:: if source <> file & 2nd paramer is empty then exit, else left-shift and loop
IF NOT EXIST "%~1" IF ""=="%~2" ( EXIT /B 0 ) ELSE SHIFT & GOTO :getLST
:: remove file from source, display filename, call youtube-dl
CALL SET source=%%source:%1=%%
ECHO Fetching URLs from %1
ECHO.
:: create new target using list.txt filename and enter new target
IF DEFINED MakeListDir MD "%TargetFolder%\%~n1" >NUL 2>&1
IF DEFINED MakeListDir PUSHD "%TargetFolder%\%~n1"
FOR /F "usebackq tokens=*" %%A IN ("%~1") DO CALL :doYTDL "%%~A"
:: return to target folder, left-shift parameters, and loop
IF DEFINED MakeListDir POPD
SHIFT
GOTO :getLST
:doYTDL url[.txt] [...]
:: exit if no parameters, display commandline, call YouTube-dl.exe
IF ""=="%~1" EXIT /B 0
ECHO youtube-dl.exe%Options%%Network%%GeoRestrict%%VideoSelect%%Download%%FileSystem%%Thumbnail%%Verbosity%%WorkArounds%%VideoFormat%%Subtitle%%Authenticate%%AdobePass%%PostProcess%%OutTemplate% %*
ECHO.
youtube-dl.exe%Options%%Network%%GeoRestrict%%VideoSelect%%Download%%FileSystem%%Thumbnail%%Verbosity%%WorkArounds%%VideoFormat%%Subtitle%%Authenticate%%AdobePass%%PostProcess%%OutTemplate% %*
SET appErr=%ERRORLEVEL%
:: capture errorlevel and display warning if non-zero
:: 0=no error, 1=invalid url/missing file, 2=no arguments/no url
IF %appErr% NEQ 0 ECHO. & ECHO ERROR: YouTube-dl.exe ErrorLevel is %appErr%. & ECHO.
ECHO -------------------------------------------------------------------------------
ECHO.
EXIT /B 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment