J2897

Easy youtube-dl

Jul 8th, 2018 (edited)
1,074
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 12.92 KB | None | 0 0
  1. :: Released under the GNU General Public License version 3+ by J2897.
  2.  
  3. @echo OFF
  4. color 1b
  5. pushd "%~dp0"
  6. title Easy youtube-dl
  7. setlocal
  8. cls
  9.  
  10. REM #################### README #################### > BEGIN
  11. :: Filename:    easy_youtube-dl.bat
  12. :: Version:     0.9.1
  13. :: Flowchart:   https://www.dropbox.com/s/8f2lunt8qazhnj2/Easy%20youtube-dl.odg
  14. :: Latest:      https://pastebin.com/26g6TDfL
  15. :: Contact:     https://pastebin.com/message_compose?to=J2897
  16. ::
  17. :: The "youtube-dl" utility is a very powerful tool. However, if you just want to
  18. :: grab a video, or download a YouTube playlist - either as MP4 videos or MP3
  19. :: audio files - then this Batch script may make things much easier.
  20. ::
  21. :: "If people are free to record visuals and audio in public, they should be free
  22. :: to record visuals and audio in the privacy of their own homes too." - J2897
  23. ::
  24. :: https://github.com/rg3/youtube-dl/blob/master/README.md#readme
  25. :: https://www.slashgeek.net/2016/06/24/5-youtube-dl-tips-might-not-know/
  26. :: https://ffmpeg.zeranoe.com/builds/
  27. ::
  28. :: NOTE: If you want to update FFmpeg at any point, just delete the 'ffmpeg' folder
  29. :: and the latest version will be automatically grabbed again.
  30. REM #################### README #################### > END
  31.  
  32. REM Set the location to store the youtube-dl and FFmpeg utilities.
  33. set "PROG_DIR=%USERPROFILE%\Programs"
  34.  
  35. REM Set the destination folder for video or audio.
  36. set "VIDEO=%USERPROFILE%\Desktop\Videos"
  37. set "AUDIO=%USERPROFILE%\Desktop\Music"
  38.  
  39. REM Set the youtube-dl variables.
  40. set "YTDL_SITE=http://yt-dl.org/latest"
  41. set "YTDL_DIR=%PROG_DIR%\youtube-dl"
  42. set "YTDL_EXE=youtube-dl.exe"
  43.  
  44. REM Set the FFmpeg variables.
  45. set "FFMPEG_JSON=http://api.github.com/repos/BtbN/FFmpeg-Builds/releases/latest"
  46. set "FFMPEG_ZIP=ffmpeg-win64-gpl.zip"
  47. set "FFMPEG_DIR=%PROG_DIR%\ffmpeg"
  48. set "FFMPEG_SUB=bin"
  49. set "FFMPEG_EXE=ffmpeg.exe"
  50. set "FFMPEG_LOC=%FFMPEG_DIR%\%FFMPEG_SUB%\%FFMPEG_EXE%"
  51.  
  52. REM Set PARA to the parameter if included.
  53. set "PARA=%1"
  54.  
  55. REM Force PowerShell to use better HTTPS security.
  56. set "TLS=[Net.ServicePointManager]::SecurityProtocol = 'tls12, tls11, tls'"
  57.  
  58. REM Set some miscellaneous variables.
  59. set "TAB=   "
  60. set QUOTE="
  61.  
  62. REM Does the youtube-dl.exe file exist?
  63. if exist "%YTDL_DIR%\%YTDL_EXE%" (
  64.     REM Update the youtube-dl.exe file if it's more than two weeks old.
  65.     forfiles /p "%YTDL_DIR%" /m "%YTDL_EXE%" /d -14 >NUL 2>&1 && (
  66.         echo Updating the %YTDL_EXE% file . . .
  67.         start "" /D "%YTDL_DIR%" /W "%YTDL_DIR%\%YTDL_EXE%" -U
  68.     )
  69. ) else (
  70.     REM Create the 'youtube-dl' folder and download the youtube-dl.exe file.
  71.     if not exist "%YTDL_DIR%" (md "%YTDL_DIR%")
  72.     echo Getting the youtube-dl executable . . .
  73.     powershell -command "& { %TLS%; (New-Object Net.WebClient).DownloadFile('%YTDL_SITE%/%YTDL_EXE%', '%YTDL_DIR%\%YTDL_EXE%') }"
  74. )
  75.  
  76. REM Check again.
  77. if not exist "%YTDL_DIR%\%YTDL_EXE%" (
  78.     echo The '%YTDL_EXE%' file isn't available.
  79.     goto :End
  80. ) else (echo Available: %YTDL_DIR%\%YTDL_EXE%)
  81.  
  82. REM Does the ffmpeg.exe file exist?
  83. if not exist "%FFMPEG_LOC%" (
  84.     REM Rename the FFmpeg folder as a precaution if one already exists, which is highly unlikely.
  85.     if exist "%FFMPEG_DIR%" (ren "%FFMPEG_DIR%" "ffbackup")
  86.     REM Download FFmpeg.
  87.     echo Getting the FFmpeg archive . . .
  88.     powershell -command "& { %TLS%; $Data = Invoke-WebRequest '%FFMPEG_JSON%' | ConvertFrom-Json; $BrowserDownloadURL = $Data.assets.Where({$_.browser_download_url -like '*win64-gpl.zip'}).browser_download_url; (New-Object Net.WebClient).DownloadFile($BrowserDownloadURL, '%TEMP%\%FFMPEG_ZIP%') }"
  89.     REM Extract the FFmpeg archive.
  90.     echo Extracting . . .
  91.     powershell -command "& { Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::ExtractToDirectory( '%TEMP%\%FFMPEG_ZIP%', '%PROG_DIR%' ) }"
  92.     REM Delete the FFmpeg archive.
  93.     del "%TEMP%\%FFMPEG_ZIP%"
  94. )
  95.  
  96. REM Get the FFmpeg folder name.
  97. for /d %%d in ("%FFMPEG_DIR%*") do (set "FFMPEG_ORIG_DIR=%%d")
  98.  
  99. REM Rename that folder if it hasn't already been renamed.
  100. if not "%FFMPEG_ORIG_DIR%" == "%FFMPEG_DIR%" (
  101.     ren "%FFMPEG_ORIG_DIR%" "ffmpeg"
  102. )
  103.  
  104. REM Check again.
  105. if not exist "%FFMPEG_LOC%" (
  106.     echo The '%FFMPEG_EXE%' file isn't available.
  107.     goto :End
  108. ) else (echo Available: %FFMPEG_LOC%)
  109.  
  110. REM Show the destination folders.
  111. echo.
  112. echo VIDEO: %VIDEO%
  113. echo AUDIO: %AUDIO%
  114. echo.
  115. pause
  116.  
  117. REM Was a URL passed as a parameter?
  118. if defined PARA (
  119.     set "URL=%PARA%"
  120.     goto :Skip_input
  121. )
  122.  
  123. REM Prompt the user to input a URL.
  124. :Try_again
  125. cls
  126. echo Please enter a URL like the three below . . .
  127. echo.
  128. echo  "https://v.youku.com/v_show/id_XMzcwOTcwNTM4OA==.html"
  129. echo  "https://www.youtube.com/watch?v=LvL_LLLAb5A"
  130. echo  "https://www.youtube.com/playlist?list=PLHJlC_5EPJgkQ5YhUv5W9BkQgXHlEfNxc"
  131. :Double_quotes
  132. echo.
  133. echo It must be a proper URL wrapped in double-quotes, just like the examples.
  134. echo.
  135. set "URL="
  136. set /P URL="Paste the URL here: "
  137. if not defined URL (goto :End)
  138. :Skip_input
  139.  
  140. REM Check for double-quotes.
  141. if not [^%URL:~0,1%] == [^%QUOTE%] (
  142.     REM No openning quote.
  143.     goto :Double_quotes
  144. )
  145. if not [^%URL:~-1%] == [^%QUOTE%] (
  146.     REM No closing quote.
  147.     goto :Double_quotes
  148. )
  149.  
  150. REM Remove double-quotes from the URL.
  151. if [^%URL:~,5%]==[^"http] (
  152.     set URL=%URL:"=%
  153. ) else (
  154.     REM Not a proper URL.
  155.     goto :Double_quotes
  156. )
  157.  
  158. REM Check if the URL is a YouTube playlist.
  159. set YT_PLAYLIST=0
  160. if "%URL:~,32%"=="https://www.youtube.com/playlist" set YT_PLAYLIST=1
  161.  
  162. REM Ask a few basic questions.
  163. cls
  164. choice /c VA /m "Would you like the Video or just the Audio"
  165. if not errorlevel 2 (
  166.     set "TARGET=%VIDEO%"
  167. ) else (
  168.     set "TARGET=%AUDIO%"
  169. )
  170.  
  171. if "%TARGET%"=="%VIDEO%" (
  172.     echo.
  173.     echo  1. Up to 8K.
  174.     echo  2. Up to 4K.
  175.     echo  3. Up to 1080p.
  176.     echo  4. Up to 720p.
  177.     echo  5. Up to 480p.
  178.     echo  6. Up to 360p.
  179.     echo.
  180.     choice /c:123456 /m "Which resolution"
  181.     if errorlevel 6 (set "RES=360") else (
  182.         if errorlevel 5 (set "RES=480") else (
  183.             if errorlevel 4 (set "RES=720") else (
  184.                 if errorlevel 3 (set "RES=1080") else (
  185.                     if errorlevel 2 (set "RES=2160") else (
  186.                         if errorlevel 1 (set "RES=4320")
  187.                     )
  188.                 )
  189.             )
  190.         )
  191.     )
  192. )
  193.  
  194. REM These may need changing as the Video Container formats change over time.
  195. :: https://en.wikipedia.org/wiki/Comparison_of_video_container_formats#Information
  196. cls
  197. if "%TARGET%"=="%VIDEO%" (
  198.     echo.
  199.     echo  1. The best available.
  200.     echo  2. MP4.
  201.     echo  3. FLV.
  202.     echo  4. OGG.
  203.     echo  5. WEBM.
  204.     echo  6. MKV.
  205.     echo  7. AVI.
  206.     echo.
  207.     choice /c:1234567 /m "Which format"
  208.     if errorlevel 7 (goto :Video_Format_Seven) else (
  209.         if errorlevel 6 (goto :Video_Format_Six) else (
  210.             if errorlevel 5 (goto :Video_Format_Five) else (
  211.                 if errorlevel 4 (goto :Video_Format_Four) else (
  212.                     if errorlevel 3 (goto :Video_Format_Three) else (
  213.                         if errorlevel 2 (goto :Video_Format_Two) else (
  214.                             if errorlevel 1 (goto :Video_Format_One)
  215.                         )
  216.                     )
  217.                 )
  218.             )
  219.         )
  220.     )
  221. ) else (
  222.     echo.
  223.     echo  1. The best available.
  224.     echo  2. AAC.
  225.     echo  3. FLAC.
  226.     echo  4. MP3.
  227.     echo  5. M4A.
  228.     echo  6. Opus.
  229.     echo  7. Vorbis.
  230.     echo  8. WAV.
  231.     echo.
  232.     choice /c:12345678 /m "Which format"
  233.     if errorlevel 8 (set "EXT=wav") else (
  234.         if errorlevel 7 (set "EXT=vorbis") else (
  235.             if errorlevel 6 (set "EXT=opus") else (
  236.                 if errorlevel 5 (set "EXT=m4a") else (
  237.                     if errorlevel 4 (set "EXT=mp3") else (
  238.                         if errorlevel 3 (set "EXT=flac") else (
  239.                             if errorlevel 2 (set "EXT=aac") else (
  240.                                 if errorlevel 1 (set "EXT=best")
  241.                             )
  242.                         )
  243.                     )
  244.                 )
  245.             )
  246.         )
  247.     )
  248. )
  249. set "FORMAT=-x --audio-format %EXT%"
  250. goto :Format_selected
  251.  
  252. :Video_Format_One
  253. set FORMAT=-f "best[height<=?%RES%]"
  254. goto :Format_selected
  255.  
  256. :Video_Format_Two
  257. set "EXT=mp4"
  258. echo.
  259. echo  1. Grab the best MP4 video, the M4A audio, and merge to MP4. [QUICK]
  260. echo  2. Grab the best video, the best audio, and transcode to MP4. [SLOW]
  261. echo.
  262. choice /c:12 /m "Which option"
  263. if errorlevel 2 (
  264.     REM Best video and best audio.
  265.     set FORMAT=-f "bestvideo[height<=?%RES%]+bestaudio"
  266.     set "RV=--recode-video %EXT%"
  267. ) else (
  268.     if errorlevel 1 (
  269.         REM Best MP4 video with the M4A audio.
  270.         set FORMAT=-f "%EXT%[height<=?%RES%]+bestaudio[ext=m4a]"
  271.         set "MOF=--merge-output-format %EXT%"
  272.     )
  273. )
  274. goto :Format_selected
  275.  
  276. :Video_Format_Three
  277. set "EXT=flv"
  278. echo.
  279. echo  1. Grab the best FLV video, the AAC/MP3 audio, and merge to FLV. [QUICK]
  280. echo  2. Grab the best video, the best audio, and transcode to FLV. [SLOW]
  281. echo.
  282. choice /c:12 /m "Which option"
  283. if errorlevel 2 (
  284.     REM Transcode the best video and best audio locally.
  285.     set FORMAT=-f "bestvideo[height<=?%RES%]+bestaudio"
  286.     set "RV=--recode-video %EXT%"
  287. ) else (
  288.     if errorlevel 1 (
  289.         REM Try to merge the best FLV video with the best AAC or MP3 audio.
  290.         set FORMAT=-f "%EXT%[height<=?%RES%]+bestaudio[ext=AAC]/%EXT%[height<=?%RES%]+bestaudio[ext=MP3]"
  291.         set "MOF=--merge-output-format %EXT%"
  292.     )
  293. )
  294. goto :Format_selected
  295.  
  296. :Video_Format_Four
  297. set "EXT=ogg"
  298. echo.
  299. echo  1. Grab the best OGG video, the best audio, and merge to OGG. [QUICK]
  300. echo  2. Grab the best video, the best audio, and transcode to OGG. [SLOW]
  301. echo.
  302. choice /c:12 /m "Which option"
  303. if errorlevel 2 (
  304.     REM Transcode the best video and best audio locally.
  305.     set FORMAT=-f "bestvideo[height<=?%RES%]+bestaudio"
  306.     set "RV=--recode-video %EXT%"
  307. ) else (
  308.     if errorlevel 1 (
  309.         REM Try to merge the best OGG video with the best audio.
  310.         set FORMAT=-f "%EXT%[height<=?%RES%]+bestaudio"
  311.         set "MOF=--merge-output-format %EXT%"
  312.     )
  313. )
  314. goto :Format_selected
  315.  
  316. :Video_Format_Five
  317. set "EXT=webm"
  318. echo.
  319. echo  1. Grab the best WEBM video, the best audio, and merge to WEBM. [QUICK]
  320. echo  2. Grab the best video, the best audio, and transcode to WEBM. [SLOW]
  321. echo.
  322. choice /c:12 /m "Which option"
  323. if errorlevel 2 (
  324.     REM Transcode the best video and best audio locally.
  325.     set FORMAT=-f "bestvideo[height<=?%RES%]+bestaudio"
  326.     set "RV=--recode-video %EXT%"
  327. ) else (
  328.     if errorlevel 1 (
  329.         REM Try to merge the best WEBM video with the best audio.
  330.         set FORMAT=-f "%EXT%[height<=?%RES%]+bestaudio"
  331.         set "MOF=--merge-output-format %EXT%"
  332.     )
  333. )
  334. goto :Format_selected
  335.  
  336. :Video_Format_Six
  337. set "EXT=mkv"
  338. echo.
  339. echo  1. Grab the best MKV video, the best audio, and merge to MKV. [QUICK]
  340. echo  2. Grab the best video, the best audio, and transcode to MKV. [SLOW]
  341. echo.
  342. choice /c:12 /m "Which option"
  343. if errorlevel 2 (
  344.     REM Transcode the best video and best audio locally.
  345.     set FORMAT=-f "bestvideo[height<=?%RES%]+bestaudio"
  346.     set "RV=--recode-video %EXT%"
  347. ) else (
  348.     if errorlevel 1 (
  349.         REM Try to merge the best MKV video with the best audio.
  350.         set FORMAT=-f "%EXT%[height<=?%RES%]+bestaudio"
  351.         set "MOF=--merge-output-format %EXT%"
  352.     )
  353. )
  354. goto :Format_selected
  355.  
  356. :Video_Format_Seven
  357. set "EXT=avi"
  358. echo.
  359. echo  1. Grab the best AVI video, the best audio, and merge to AVI. [QUICK]
  360. echo  2. Grab the best video, the best audio, and transcode to AVI. [SLOW]
  361. echo.
  362. choice /c:12 /m "Which option"
  363. if errorlevel 2 (
  364.     REM Transcode the best video and best audio locally.
  365.     set FORMAT=-f "bestvideo[height<=?%RES%]+bestaudio"
  366.     set "RV=--recode-video %EXT%"
  367. ) else (
  368.     if errorlevel 1 (
  369.         REM Try to merge the best AVI video with the best audio.
  370.         set FORMAT=-f "%EXT%[height<=?%RES%]+bestaudio"
  371.         set "MOF=--merge-output-format %EXT%"
  372.     )
  373. )
  374. goto :Format_selected
  375.  
  376. :Format_selected
  377. echo.
  378. choice /c YN /m "Restrict filenames"
  379. if not errorlevel 2 (
  380.     set "RF=--restrict-filenames"
  381. ) else (
  382.     set "RF="
  383. )
  384.  
  385. REM Main command.
  386. cls
  387. echo URL:%TAB%%URL%
  388. echo Target:%TAB%%TARGET%
  389. echo.
  390. if %YT_PLAYLIST% EQU 1 (
  391.     REM Download YouTube playlist videos in separate directory indexed by video order in a playlist.
  392.     "%YTDL_DIR%\%YTDL_EXE%" --ffmpeg-location "%FFMPEG_LOC%" --prefer-insecure %FORMAT% -o "%TARGET%\%%(playlist)s\%%(playlist_index)s - %%(title)s.%%(ext)s" "%URL%" %RF% %MOF% %RV%
  393. ) else (
  394.     REM Download a single video.
  395.     "%YTDL_DIR%\%YTDL_EXE%" --ffmpeg-location "%FFMPEG_LOC%" --prefer-insecure %FORMAT% -o "%TARGET%\%%(title)s.%%(ext)s" "%URL%" %RF% %MOF% %RV%
  396. )
  397.  
  398. REM Finished, audio alert.
  399. echo 
  400.  
  401. REM Present the option to restart from the beginning.
  402. choice /c YN /m "Download another"
  403. if not errorlevel 2 (
  404.     goto :Try_again
  405. ) else (
  406.     goto :End
  407. )
  408.  
  409. REM List available formats.
  410. ::"%YTDL_DIR%\%YTDL_EXE%" --list-formats "%URL%"
  411.  
  412. REM CMD version...
  413. ::"%USERPROFILE%\Programs\youtube-dl\youtube-dl.exe" --list-formats "https://www.youtube.com/watch?v=QVov2TEYJ8k"
  414.  
  415. REM Grab the video in a specific format.
  416. ::"%YTDL_DIR%\%YTDL_EXE%" --ffmpeg-location "%FFMPEG_LOC%" --prefer-insecure -f 22 -o "%TARGET%\%%(title)s.%%(ext)s" "%URL%"
  417.  
  418. REM CMD version...
  419. ::"%USERPROFILE%\Programs\youtube-dl\youtube-dl.exe" --ffmpeg-location "%USERPROFILE%\Programs\ffmpeg\bin\ffmpeg.exe" --prefer-insecure -f 22 -o "H:\Large File Share\Videos\Training\%(title)s.%(ext)s" "https://www.youtube.com/watch?v=QVov2TEYJ8k"
  420.  
  421. REM Grab the video in a specific format and extract the audio.
  422. ::"%YTDL_DIR%\%YTDL_EXE%" --ffmpeg-location %FFMPEG_LOC% --prefer-insecure -x --audio-format mp3  -f 139 -o "%TARGET%\%%(title)s.%%(ext)s" "%URL%"
  423.  
  424. :End
  425. endlocal
  426. popd
  427. pause
  428. color
Add Comment
Please, Sign In to add comment