Skip to content

Instantly share code, notes, and snippets.

@milnak
Last active November 30, 2022 06:02
Show Gist options
  • Save milnak/1eb8f642868e4d47d1de42d4f3ec6766 to your computer and use it in GitHub Desktop.
Save milnak/1eb8f642868e4d47d1de42d4f3ec6766 to your computer and use it in GitHub Desktop.
[youtube-dl helper script] CMD script that wraps yt-dlp to simply downloading #youtube
@echo off
REM
REM Script to assist in downloading using yt-dlp
REM
REM Usage:
REM youtubedl.cmd {f,m,v,list-extractors} {video_url}
REM youtubedl.cmd update
REM
REM f = flac
REM m = mkv
REM v = mp4
REM list-extractors = list extractors
REM update = update yt-dlp (no video_url required)
REM
REM video_url:
REM Full url: https://www.youtube.com/watch?v=yYIWoj3uO08
REM Video ID: yYIWoj3uO08
REM
setlocal
REM Hidden ESC character on next line.
set ESC=
GOTO :main
REM Bright RED on Black
:ECHO_R
echo %ESC%[1;31m%~1%ESC%[0m
GOTO :EOF
REM Bright Yellow on Black
:ECHO_Y
echo %ESC%[1;33m%~1%ESC%[0m
GOTO :EOF
:MAIN
set APP=yt-dlp.exe
rem if /i "%~1" equ "update" ( %APP% --verbose --update & goto :EOF )
if /i "%~1" equ "update" (
CALL :ECHO_Y "Updating..."
python.exe -m pip install --upgrade pip
python.exe -m pip install --upgrade %APP%
goto :EOF
)
if /i "%~1" equ "list-extractors" ( %APP% --list-extractors & goto :EOF )
REM default format is mkv
if /i "%~1" equ "f" goto out_ok
if /i "%~1" equ "m" goto out_ok
if /i "%~1" equ "v" goto out_ok
call :ECHO_R "Specify output format {f,m,v}; update; list-extractors; list-formats [URL]"
goto :EOF
:out_ok
if "%~2" equ "" (
call :ECHO_R "Specify YouTube URL or video ID"
goto :EOF
)
echo."%~2" | findstr /C:"://" >nul
if errorlevel 1 (
REM Assume only video ID was provided.
REM Useful for Powershell where calling CMD from PowerShell will remove everything after =
set URL=https://www.youtube.com/watch?v=%~2
) ELSE (
IF /i "%~2" equ "https://www.youtube.com/watch?v" (
REM PowerShell will split:
REM youtubedl f 'https://www.youtube.com/watch?v=yYIWoj3uO08'
REM to youtubedl.cmd 'f' 'https://www.youtube.com/watch?v' 'yYIWoj3uO08'
call :ECHO_Y "Called from PowerShell. Will use video ID %3"
set URL=https://www.youtube.com/watch?v=%~3
) ELSE (
REM URL was provided.
set URL=%~2
)
)
if /i "%~1" equ "list-formats" ( %APP% --list-formats %~2 & goto :EOF )
%APP% --version
if errorlevel 1 (
call :ECHO_R "Unable to start %APP%"
goto :EOF
)
REM Video Selection
REM ---------------
SET VIDSEL_OPTS=
If NOT "%URL%"=="%URL:/playlist=%" (
REM CALL :ECHO_Y "WARNING: Playlist requested. Only downloading previous 3 months of videos."
REM set VIDSEL_OPTS=--dateafter now-3month
)
REM Filesystem Options
REM ------------------
REM e.g. "Where the Sun Goes ft. Stevie Wonder [TrVLu9p65a0].flac"
set FS_OPTS=--output "%%(title)s [%%(id)s].%%(ext)s"
if exist "%USERPROFILE%\downloads\youtube.com_cookies.txt" (
REM Avoid 429
REM https://microsoftedge.microsoft.com/addons/detail/get-cookiestxt/helleheikohejgehaknifdkcfcmceeip
CALL :ECHO_Y "Cookie file found. Using YouTube cookies."
set FS_OPTS=%FS_OPTS% --cookies "%USERPROFILE%\downloads\youtube.com_cookies.txt"
)
REM Verbosity and Simulation Options
REM --------------------------------
REM --print-traffic
REM removed "--add-metadata" as it was redownloading metadata for files that were already downloaded.
rem set VERBOS_OPTS=--no-simulate --print "%%(uploader)s/%%(playlist)s/%%(playlist_index)s/%%(title)s"
REM set VERBOS_OPTS=--quiet --progress --no-simulate --print "[%%(playlist_index)s] %%(title)s"
set VERBOS_OPTS=--quiet --progress --no-simulate --print "[%%(n_entries+1-playlist_index)d] %%(title)s"
REM Post-Processing Options
REM -----------------------
REM These overrides aren't recommended anymore.
REM set POSTPROC_OPTS="bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4"
REM set POSTPROC_OPTS=-f best
set POSTPROC_OPTS=
if /I "%~1" equ "f" set POSTPROC_OPTS=--extract-audio --audio-format flac
if /I "%~1" equ "m" set POSTPROC_OPTS=--extract-audio --audio-format mp3
REM Prefer mp4 over webm
if /I "%~1" equ "v" set POSTPROC_OPTS=--recode-video mp4
echo.
@echo on
%APP% %VIDSEL_OPTS% %FS_OPTS% %VERBOS_OPTS% %POSTPROC_OPTS% "%URL%"
@echo off
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment