J2897 icon

Easy youtube-dl

J2897 | PRO | 12/18/20 09:26:04 PM UTC (Edited) | 0 ⭐ | 1583 👁️ | Never ⏰ | []
Batch |

12.92 KB

|

None

|

0 👍

/

0 👎

:: Released under the GNU General Public License version 3+ by J2897.
 
@echo OFF
color 1b
pushd "%~dp0"
title Easy youtube-dl
setlocal
cls
 
REM #################### README #################### > BEGIN
:: Filename:    easy_youtube-dl.bat
:: Version:     0.9.1
:: Flowchart:   https://www.dropbox.com/s/8f2lunt8qazhnj2/Easy%20youtube-dl.odg
:: Latest:      https://pastebin.com/26g6TDfL
:: Contact:     https://pastebin.com/message_compose?to=J2897
::
:: The "youtube-dl" utility is a very powerful tool. However, if you just want to
:: grab a video, or download a YouTube playlist - either as MP4 videos or MP3
:: audio files - then this Batch script may make things much easier.
::
:: "If people are free to record visuals and audio in public, they should be free 
:: to record visuals and audio in the privacy of their own homes too." - J2897
::
:: https://github.com/rg3/youtube-dl/blob/master/README.md#readme
:: https://www.slashgeek.net/2016/06/24/5-youtube-dl-tips-might-not-know/
:: https://ffmpeg.zeranoe.com/builds/
::
:: NOTE: If you want to update FFmpeg at any point, just delete the 'ffmpeg' folder
:: and the latest version will be automatically grabbed again.
REM #################### README #################### > END
 
REM Set the location to store the youtube-dl and FFmpeg utilities.
set "PROG_DIR=%USERPROFILE%\Programs"
 
REM Set the destination folder for video or audio.
set "VIDEO=%USERPROFILE%\Desktop\Videos"
set "AUDIO=%USERPROFILE%\Desktop\Music"
 
REM Set the youtube-dl variables.
set "YTDL_SITE=http://yt-dl.org/latest"
set "YTDL_DIR=%PROG_DIR%\youtube-dl"
set "YTDL_EXE=youtube-dl.exe"
 
REM Set the FFmpeg variables.
set "FFMPEG_JSON=http://api.github.com/repos/BtbN/FFmpeg-Builds/releases/latest"
set "FFMPEG_ZIP=ffmpeg-win64-gpl.zip"
set "FFMPEG_DIR=%PROG_DIR%\ffmpeg"
set "FFMPEG_SUB=bin"
set "FFMPEG_EXE=ffmpeg.exe"
set "FFMPEG_LOC=%FFMPEG_DIR%\%FFMPEG_SUB%\%FFMPEG_EXE%"
 
REM Set PARA to the parameter if included.
set "PARA=%1"
 
REM Force PowerShell to use better HTTPS security.
set "TLS=[Net.ServicePointManager]::SecurityProtocol = 'tls12, tls11, tls'"
 
REM Set some miscellaneous variables.
set "TAB=   "
set QUOTE="
 
REM Does the youtube-dl.exe file exist?
if exist "%YTDL_DIR%\%YTDL_EXE%" (
    REM Update the youtube-dl.exe file if it's more than two weeks old.
    forfiles /p "%YTDL_DIR%" /m "%YTDL_EXE%" /d -14 >NUL 2>&1 && (
        echo Updating the %YTDL_EXE% file . . .
        start "" /D "%YTDL_DIR%" /W "%YTDL_DIR%\%YTDL_EXE%" -U
    )
) else (
    REM Create the 'youtube-dl' folder and download the youtube-dl.exe file.
    if not exist "%YTDL_DIR%" (md "%YTDL_DIR%")
    echo Getting the youtube-dl executable . . .
    powershell -command "& { %TLS%; (New-Object Net.WebClient).DownloadFile('%YTDL_SITE%/%YTDL_EXE%', '%YTDL_DIR%\%YTDL_EXE%') }"
) 
 
REM Check again.
if not exist "%YTDL_DIR%\%YTDL_EXE%" (
    echo The '%YTDL_EXE%' file isn't available.
    goto :End
) else (echo Available: %YTDL_DIR%\%YTDL_EXE%)
 
REM Does the ffmpeg.exe file exist?
if not exist "%FFMPEG_LOC%" (
    REM Rename the FFmpeg folder as a precaution if one already exists, which is highly unlikely.
    if exist "%FFMPEG_DIR%" (ren "%FFMPEG_DIR%" "ffbackup")
    REM Download FFmpeg.
    echo Getting the FFmpeg archive . . .
    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%') }"
    REM Extract the FFmpeg archive.
    echo Extracting . . .
    powershell -command "& { Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::ExtractToDirectory( '%TEMP%\%FFMPEG_ZIP%', '%PROG_DIR%' ) }"
    REM Delete the FFmpeg archive.
    del "%TEMP%\%FFMPEG_ZIP%"
)
 
REM Get the FFmpeg folder name.
for /d %%d in ("%FFMPEG_DIR%*") do (set "FFMPEG_ORIG_DIR=%%d")
 
REM Rename that folder if it hasn't already been renamed.
if not "%FFMPEG_ORIG_DIR%" == "%FFMPEG_DIR%" (
    ren "%FFMPEG_ORIG_DIR%" "ffmpeg"
)
 
REM Check again. 
if not exist "%FFMPEG_LOC%" (
    echo The '%FFMPEG_EXE%' file isn't available.
    goto :End
) else (echo Available: %FFMPEG_LOC%)
 
REM Show the destination folders.
echo.
echo VIDEO: %VIDEO%
echo AUDIO: %AUDIO%
echo.
pause
 
REM Was a URL passed as a parameter?
if defined PARA (
    set "URL=%PARA%"
    goto :Skip_input
)
 
REM Prompt the user to input a URL.
:Try_again
cls
echo Please enter a URL like the three below . . .
echo.
echo  "https://v.youku.com/v_show/id_XMzcwOTcwNTM4OA==.html"
echo  "https://www.youtube.com/watch?v=LvL_LLLAb5A"
echo  "https://www.youtube.com/playlist?list=PLHJlC_5EPJgkQ5YhUv5W9BkQgXHlEfNxc"
:Double_quotes
echo.
echo It must be a proper URL wrapped in double-quotes, just like the examples.
echo.
set "URL="
set /P URL="Paste the URL here: "
if not defined URL (goto :End)
:Skip_input
 
REM Check for double-quotes.
if not [^%URL:~0,1%] == [^%QUOTE%] (
    REM No openning quote.
    goto :Double_quotes
)
if not [^%URL:~-1%] == [^%QUOTE%] (
    REM No closing quote.
    goto :Double_quotes
)
 
REM Remove double-quotes from the URL.
if [^%URL:~,5%]==[^"http] (
    set URL=%URL:"=%
) else (
    REM Not a proper URL.
    goto :Double_quotes
)
 
REM Check if the URL is a YouTube playlist.
set YT_PLAYLIST=0
if "%URL:~,32%"=="https://www.youtube.com/playlist" set YT_PLAYLIST=1
 
REM Ask a few basic questions.
cls
choice /c VA /m "Would you like the Video or just the Audio"
if not errorlevel 2 (
    set "TARGET=%VIDEO%"
) else (
    set "TARGET=%AUDIO%"
)
 
if "%TARGET%"=="%VIDEO%" (
    echo.
    echo  1. Up to 8K.
    echo  2. Up to 4K.
    echo  3. Up to 1080p.
    echo  4. Up to 720p.
    echo  5. Up to 480p.
    echo  6. Up to 360p.
    echo.
    choice /c:123456 /m "Which resolution"
    if errorlevel 6 (set "RES=360") else (
        if errorlevel 5 (set "RES=480") else (
            if errorlevel 4 (set "RES=720") else (
                if errorlevel 3 (set "RES=1080") else (
                    if errorlevel 2 (set "RES=2160") else (
                        if errorlevel 1 (set "RES=4320")
                    )
                )
            )
        )
    )
)
 
REM These may need changing as the Video Container formats change over time.
:: https://en.wikipedia.org/wiki/Comparison_of_video_container_formats#Information
cls
if "%TARGET%"=="%VIDEO%" (
    echo.
    echo  1. The best available.
    echo  2. MP4.
    echo  3. FLV.
    echo  4. OGG.
    echo  5. WEBM.
    echo  6. MKV.
    echo  7. AVI.
    echo.
    choice /c:1234567 /m "Which format"
    if errorlevel 7 (goto :Video_Format_Seven) else (
        if errorlevel 6 (goto :Video_Format_Six) else (
            if errorlevel 5 (goto :Video_Format_Five) else (
                if errorlevel 4 (goto :Video_Format_Four) else (
                    if errorlevel 3 (goto :Video_Format_Three) else (
                        if errorlevel 2 (goto :Video_Format_Two) else (
                            if errorlevel 1 (goto :Video_Format_One)
                        )
                    )
                )
            )
        )
    )
) else (
    echo.
    echo  1. The best available.
    echo  2. AAC.
    echo  3. FLAC.
    echo  4. MP3.
    echo  5. M4A.
    echo  6. Opus.
    echo  7. Vorbis.
    echo  8. WAV.
    echo.
    choice /c:12345678 /m "Which format"
    if errorlevel 8 (set "EXT=wav") else (
        if errorlevel 7 (set "EXT=vorbis") else (
            if errorlevel 6 (set "EXT=opus") else (
                if errorlevel 5 (set "EXT=m4a") else (
                    if errorlevel 4 (set "EXT=mp3") else (
                        if errorlevel 3 (set "EXT=flac") else (
                            if errorlevel 2 (set "EXT=aac") else (
                                if errorlevel 1 (set "EXT=best")
                            )
                        )
                    )
                )
            )
        )
    )
)
set "FORMAT=-x --audio-format %EXT%"
goto :Format_selected
 
:Video_Format_One
set FORMAT=-f "best[height<=?%RES%]"
goto :Format_selected
 
:Video_Format_Two
set "EXT=mp4"
echo.
echo  1. Grab the best MP4 video, the M4A audio, and merge to MP4. [QUICK]
echo  2. Grab the best video, the best audio, and transcode to MP4. [SLOW]
echo.
choice /c:12 /m "Which option"
if errorlevel 2 (
    REM Best video and best audio.
    set FORMAT=-f "bestvideo[height<=?%RES%]+bestaudio"
    set "RV=--recode-video %EXT%"
) else (
    if errorlevel 1 (
        REM Best MP4 video with the M4A audio.
        set FORMAT=-f "%EXT%[height<=?%RES%]+bestaudio[ext=m4a]"
        set "MOF=--merge-output-format %EXT%"
    )
)
goto :Format_selected
 
:Video_Format_Three
set "EXT=flv"
echo.
echo  1. Grab the best FLV video, the AAC/MP3 audio, and merge to FLV. [QUICK]
echo  2. Grab the best video, the best audio, and transcode to FLV. [SLOW]
echo.
choice /c:12 /m "Which option"
if errorlevel 2 (
    REM Transcode the best video and best audio locally.
    set FORMAT=-f "bestvideo[height<=?%RES%]+bestaudio"
    set "RV=--recode-video %EXT%"
) else (
    if errorlevel 1 (
        REM Try to merge the best FLV video with the best AAC or MP3 audio.
        set FORMAT=-f "%EXT%[height<=?%RES%]+bestaudio[ext=AAC]/%EXT%[height<=?%RES%]+bestaudio[ext=MP3]"
        set "MOF=--merge-output-format %EXT%"
    )
)
goto :Format_selected
 
:Video_Format_Four
set "EXT=ogg"
echo.
echo  1. Grab the best OGG video, the best audio, and merge to OGG. [QUICK]
echo  2. Grab the best video, the best audio, and transcode to OGG. [SLOW]
echo.
choice /c:12 /m "Which option"
if errorlevel 2 (
    REM Transcode the best video and best audio locally.
    set FORMAT=-f "bestvideo[height<=?%RES%]+bestaudio"
    set "RV=--recode-video %EXT%"
) else (
    if errorlevel 1 (
        REM Try to merge the best OGG video with the best audio.
        set FORMAT=-f "%EXT%[height<=?%RES%]+bestaudio"
        set "MOF=--merge-output-format %EXT%"
    )
)
goto :Format_selected
 
:Video_Format_Five
set "EXT=webm"
echo.
echo  1. Grab the best WEBM video, the best audio, and merge to WEBM. [QUICK]
echo  2. Grab the best video, the best audio, and transcode to WEBM. [SLOW]
echo.
choice /c:12 /m "Which option"
if errorlevel 2 (
    REM Transcode the best video and best audio locally.
    set FORMAT=-f "bestvideo[height<=?%RES%]+bestaudio"
    set "RV=--recode-video %EXT%"
) else (
    if errorlevel 1 (
        REM Try to merge the best WEBM video with the best audio.
        set FORMAT=-f "%EXT%[height<=?%RES%]+bestaudio"
        set "MOF=--merge-output-format %EXT%"
    )
)
goto :Format_selected
 
:Video_Format_Six
set "EXT=mkv"
echo.
echo  1. Grab the best MKV video, the best audio, and merge to MKV. [QUICK]
echo  2. Grab the best video, the best audio, and transcode to MKV. [SLOW]
echo.
choice /c:12 /m "Which option"
if errorlevel 2 (
    REM Transcode the best video and best audio locally.
    set FORMAT=-f "bestvideo[height<=?%RES%]+bestaudio"
    set "RV=--recode-video %EXT%"
) else (
    if errorlevel 1 (
        REM Try to merge the best MKV video with the best audio.
        set FORMAT=-f "%EXT%[height<=?%RES%]+bestaudio"
        set "MOF=--merge-output-format %EXT%"
    )
)
goto :Format_selected
 
:Video_Format_Seven
set "EXT=avi"
echo.
echo  1. Grab the best AVI video, the best audio, and merge to AVI. [QUICK]
echo  2. Grab the best video, the best audio, and transcode to AVI. [SLOW]
echo.
choice /c:12 /m "Which option"
if errorlevel 2 (
    REM Transcode the best video and best audio locally.
    set FORMAT=-f "bestvideo[height<=?%RES%]+bestaudio"
    set "RV=--recode-video %EXT%"
) else (
    if errorlevel 1 (
        REM Try to merge the best AVI video with the best audio.
        set FORMAT=-f "%EXT%[height<=?%RES%]+bestaudio"
        set "MOF=--merge-output-format %EXT%"
    )
)
goto :Format_selected
 
:Format_selected
echo.
choice /c YN /m "Restrict filenames"
if not errorlevel 2 (
    set "RF=--restrict-filenames"
) else (
    set "RF="
)
 
REM Main command.
cls
echo URL:%TAB%%URL%
echo Target:%TAB%%TARGET%
echo.
if %YT_PLAYLIST% EQU 1 (
    REM Download YouTube playlist videos in separate directory indexed by video order in a playlist.
    "%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%
) else (
    REM Download a single video.
    "%YTDL_DIR%\%YTDL_EXE%" --ffmpeg-location "%FFMPEG_LOC%" --prefer-insecure %FORMAT% -o "%TARGET%\%%(title)s.%%(ext)s" "%URL%" %RF% %MOF% %RV%
)
 
REM Finished, audio alert.
echo 
 
REM Present the option to restart from the beginning.
choice /c YN /m "Download another"
if not errorlevel 2 (
    goto :Try_again
) else (
    goto :End
)
 
REM List available formats.
::"%YTDL_DIR%\%YTDL_EXE%" --list-formats "%URL%"
 
REM CMD version...
::"%USERPROFILE%\Programs\youtube-dl\youtube-dl.exe" --list-formats "https://www.youtube.com/watch?v=QVov2TEYJ8k"
 
REM Grab the video in a specific format.
::"%YTDL_DIR%\%YTDL_EXE%" --ffmpeg-location "%FFMPEG_LOC%" --prefer-insecure -f 22 -o "%TARGET%\%%(title)s.%%(ext)s" "%URL%"
 
REM CMD version...
::"%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"
 
REM Grab the video in a specific format and extract the audio.
::"%YTDL_DIR%\%YTDL_EXE%" --ffmpeg-location %FFMPEG_LOC% --prefer-insecure -x --audio-format mp3  -f 139 -o "%TARGET%\%%(title)s.%%(ext)s" "%URL%"
 
:End
endlocal
popd
pause
color

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎