Skip to content

Instantly share code, notes, and snippets.

@mo-han
Created July 7, 2019 06:41
Show Gist options
  • Save mo-han/6f6d79b8c37fa8079db9d9045d3e1526 to your computer and use it in GitHub Desktop.
Save mo-han/6f6d79b8c37fa8079db9d9045d3e1526 to your computer and use it in GitHub Desktop.
a windows cmd script wrapper for youtube-dl
:: yt-dl.bat
:: Mo Han <[email protected]>
:: @ yt-dl_worker.bat
@echo off
setlocal
call u_var.bat
set proxy=%u_autoproxy%
rem set proxy=%u_proxy%
pushd "%u_dl%"
set default=false
set worker=start "" cmd /c "yt-dl_worker.bat || pause"
if "%1"=="m" goto :push_default
if "%1"=="w" goto :push_default
if "%1"=="b" goto :push_default
:pop_default
set "args=%*"
if "%1"=="" (
goto :i_main_loop
) else (
call set "url=%%args:*%default% =%%"
%worker%
popd
goto :eof
)
:push_default
set default=%1
set worker=start /min "" cmd /c "yt-dl_worker.bat || pause"
shift
goto :pop_default
:i_main_loop
set url=
echo INPUT URL TO DOWNLOAD ([q] QUIT):
set /p url=#URL:
if "%url%"=="q" (
popd
goto :eof
)
if not "%url%"=="" (
%worker%
)
goto :i_main_loop
:: Changelog
:: [0.2] - 2017-12-12
:: + Three possible options: `m`, `w`, and `b`
:: 170620
:: + New param `d` for "automatically select default" (will not prompt for format).
:: 170430
:: + One-shot downloading with url provided as param.
:: 160906
:: [*] set _proxy=%u_proxy%
:: yt-dl_worker.bat
:: Mo Han <[email protected]>
:: @ yt-dl.bat, python.exe (v3), youtube-dl.exe
@echo off
setlocal
title "%url%"
set /a retry=0
set retry_max=3
set base_args_uploader=-o "%%(title)s [%%(id)s][%%(uploader)s].%%(ext)s" --yes-playlist "%url%"
set base_args_iwara=-o "%%(title)s [%%(id)s][%%(uploader)s].%%(ext)s" --yes-playlist "%url%"
set arial2_args=--external-downloader aria2c --external-downloader-args "-x3 -s3 -k 1M"
set arial2_proxy_args=--proxy=%proxy% %arial2_args%
rem set arial2_proxy_args=--proxy=%proxy% %arial2_args% --external-downloader-args "--all-proxy=%proxy% -x10 -s10"
set args=%arial2_proxy_args% %base_args_uploader%
echo "%url%" | findstr "iwara" > nul
rem if %errorlevel%==0 set args=--no-check-certificate %args%
if %errorlevel%==0 set args=%arial2_proxy_args% %base_args_iwara%
echo "%url%" | findstr "bilibili" > nul
if %errorlevel%==0 set args=%arial2_args% %base_args_uploader%
rem Append `--no-check-certificate` for YouTube. Have no idea but it works. And since it's just video data downloaded, there should be no security/privacy issue.
rem echo "%url%" | findstr "youtube youtu.be" > nul
rem if %errorlevel%==0 set args=--no-check-certificate %args%
echo %args%
echo --------------------------------
:prompt
set fmt=
echo [Q]uit, [B]est, [F]ormat list (Default), [Enter]=Default
echo [M] try mp4 1080p 60fps
echo [W] try webm 1080p 60fps
if %default%==false (set /p "fmt=> ") else (set fmt=%default%)
echo --------------------------------
rem youtube-dl.exe --get-filename -q %args%
rem echo --------------------------------
if not defined fmt set fmt=f
if "%fmt%"=="q" exit
if "%fmt%"=="f" goto :formats
if "%fmt%"=="b" set "fmt=bestvideo+bestaudio/best"
if "%fmt%"=="m" set "fmt=(mp4/webm)[height<=1080][fps<=60]+(aac/m4a)/bestvideo+bestaudio/best"
if "%fmt%"=="w" set "fmt=(webm/mp4)[height<=1080][fps<=60]+(opus/webm/vorbis)/bestvideo+bestaudio/best"
set args=-f "%fmt%" %args%
goto :download
:download
echo %args%
youtube-dl.exe %args%
:: Success or failure?
if errorlevel 1 (
set /a retry+=1
if %retry% lss %retry_max% goto :download
set /p "_fin=[R]e-download or [q]uit ? "
if not defined _fin goto :download
if "%_fin%"=="r" goto :download
if "%_fin%"=="q" goto :end
) else (
echo --------------------------------
echo DOWNLOAD SUCCESS
timeout 3
goto :end
)
goto :end
:formats
youtube-dl.exe -F %args%
echo --------------------------------
goto :prompt
:end
exit
:: Changelog
:: [0.3.0] - 2019-06-28
:: * a lot of minor changes been forgotten
:: [0.2.2] 2018-02-14
:: Append `--no-check-certificate` for YouTube. Reasons unknown, but the CRL url `http://pki.google.com/GIAG2.crl` in YouTube's SSL cert is blocked by GFW, which might be the cause.
:: [0.2.1] - 2018-02-08
:: add: `--external-downloader-args "--http-proxy=%proxy% -x10 -s10"` for aria2.
:: [0.2] - 2017-12-12
:: * Now default to [F]ormat list, pressing [Enter] will list all formats.
:: 171120
:: * Default to [W] webm/mp4 1080p 60fps bestaudio (Default)
:: 170620
:: + New param `d` for "automatically select default" (will not prompt for format).
:: 170517
:: * Format list is optional now. It would only be showed chosen.
:: 170514
:: * `arial2_args` & `arial2_proxy_args`
:: 170501
:: + Auto retry downloading for 3 times, then prompt for retrying.
:: 170414
:: [*] MP4 seems better (although bigger) than WebM on YouTube
:: [*] [1] mp4/webm 1080p 60fps bestaudio (default)
:: [*] [2] webm/mp4 1080p 60fps bestaudio
:: 170331
:: [+] Use aria2c to download Bilibili videos (to avoid speed limitation)
:: [+] New download choice: webm/mp4 [1]920 60fps bestaudio (default)
:: [+] New download choice: webm/mp4 [2]560 60fps bestaudio
:: [+] Wait 5 sec to exit after download
:: 170316
:: [+] Always get filename
:: [-] Remove `fn`
:: [+] Input `q` to quit
:: [+] After an error occurs, input `r` to re-download
:: 161113
:: [+] Input `fn` to get output file name
:: [+] Exam 'bilibili' in URL to disable proxy
:: [*] Use proxy by default
:: [*] Quote URL for stablity against special characters such as ampersand
:: 160916
:: [-] Remove `IF` block of proxy switching
:: [*] Always use proxy
:: 160906
:: [*] NOT useing proxy by default
:: [-] Remove `--no-playlist` option
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment