1

Please view the batch file below.

The goal

... is to run yt-dlp in a batch file, which then gives you a menu with six options:

  • yt-dlp "%temp_file%"
  • yt-dlp -f best "%temp_file%"
  • yt-dlp --write-sub "%temp_file%"
  • yt-dlp --sub-lang en "%temp_file%"
  • yt-dlp -cit "%temp_file%"
  • yt-dlp -x --audio-format mp3 "%temp_file%"

The method I came up with was to use a variable, which I called temp_file, which would be filled with the contents of the clipboard, this is the URL of a youtube video, which would be passed on, (piped to?), the yt-dlp program on the command-line. But it regrettably doesn't work. The problem might be in step B. Copy the contents of the clipboard to the temporary file. This doesn't happen, at all.

What I tried to do step B:

  • using Powershell Command-let Get-clipboard
  • the CLIP.EXE application
  • creating a temp file

But these don't work, or, I've done something wrong. The error message is: "The syntax of the command is incorrect."

Question:

Can you tell me where the batch file below goes wrong and produces this error message? And how to fix it so that it accepts the contents of the clip board and pipes the content of that ... to Yt-Dlp?

set "temp_file=%TEMP%\%RANDOM%"

EDIT :: B. Copy the contents of the clipboard to the temporary file powershell -command "Add-Type -AssemblyName PresentationCore; [Windows.Clipboard]::GetText() | Out-File -FilePath 'temp_file.txt'"

:: clip < NUL > "%temp_file%" :: I left this in by accident, sorry about that. This only puts things :: on the clipboard, while I thought this would get things out of :: the clipboard. Which doesn't seem so unreasonable?

:: Display the menu echo Please select an option: echo 1. Download with yt-dlp echo 2. Download with yt-dlp (best quality) echo 3. Download with yt-dlp (with subtitles) echo 4. Download with yt-dlp (with English subtitles) echo 5. Download with yt-dlp (with captions) echo 6. Download with yt-dlp (audio only)

:: Get user input set /p choice=Enter your choice:

:: Process the user's choice if "%choice%"=="1" ( yt-dlp "%temp_file%" ) else if "%choice%"=="2" ( yt-dlp -f best "%temp_file%" ) else if "%choice%"=="3" ( yt-dlp --write-sub "%temp_file%" ) else if "%choice%"=="4" ( yt-dlp --sub-lang en "%temp_file%" ) else if "%choice%"=="5" ( yt-dlp -cit "%temp_file%" ) else if "%choice%"=="6" ( yt-dlp -x --audio-format mp3 "%temp_file%" ) else ( echo Invalid choice. Please try again. )

:: Pause the script so that you can see the output pause

:: Delete the temporary file del "%temp_file%"

:: Exit the script exit /B```

2 Answers2

1
@echo off && setlocal EnableDelayedExpansion

set^ "_options=;,-f b,--write-sub,--sub-lang en"
set^ "_options=!_options!,--newline,-x --audio-format mp3"
set^ _x0="%_options:,=" & set^ /a _i+=1+0 & set^ "_x!_i!=%"

cd /d "%~dp0" & for /f usebackq^delims^= %%i in =;(`
     "powershell -c gcb ^| Select-Object -First 1"
       `);= do set^ "_yt-dl=.\yt-dlp.exe "%%~i""

if not "!_yt-dl!" == "" =;( for /f "usebackq delims=" %%G in =;(`
     type "%~f0" ^| findstr /b \:\:`);= do echo/%%G & set "_x0= "
    );= else endlocal && goto :eOf 

set /p "_choice=:: Enter your choice: "
echo/%_choice%| findstr /be [0-5] >nul && =;( 
     call !_yt-dl! %%_x!_choice!%% | findstr ]
    );= || echo/Invalid choice. Please try again^^^!!..

endlocal

:: Please select an option:
:: [0] Download with yt-dlp
:: [1] Download with yt-dlp (best quality)
:: [2] Download with yt-dlp (with subtitles)
:: [3] Download with yt-dlp (with English subtitles)
:: [4] Download with yt-dlp (newline intead "-cit with captions" )
:: [5] Download with yt-dlp (audio only)


1. You don't need to open a file, write to it, read it and then process it and delete it just to get the contents of the clipboard...


2. There is a windows cmd command called choice, avoiding using a command name when defining a variable could be a suggestion...


3. The -f best option brings a long warning suggesting the use of -f b, consider replacing for something appropriate as an expected result without unnecessary warnings.


4. In the current version of yt-dlp.exe I did not find the -cit flag (option 5). Just to create 6 menu items, I replaced them with a -newline, and also to avoid repetitive error messages when carrying out tests.


5. Preventing any line breaks from coming with the copied link, considering that it can be obtained in another location other than the field in the browser or a link on a website, would be a way to avoid surprises.


6. Changing menu items from [1-6] to [0-5] is just a way to make the substrings in variable replace auto-expanded more easily.


7. At some point, go into the folder where the bat is located (and I think you have the other binaries there too), it would be useful, or define the paths if you don't already have them defined in the system variable.


Additional resources:

Io-oI
  • 9,237
0

I think I found it. The MS-DOS batch file which downloads youtube videos when you've copied the URL to the clipboard, looks like this:

@echo off
setlocal EnableDelayedExpansion
echo  setlocal EnableDelayedExpansion
pause

:: B. Copy the contents of the clipboard to the temporary file FOR /F "usebackq delims=" %%i IN (powershell -command &quot;Get-Clipboard&quot;) DO SET temp_file=%%i

echo %temp_file% pause

:: C. Display the menu echo Please select an option: echo 1. Download with yt-dlp echo 2. Download with yt-dlp (best quality) echo 3. Download with yt-dlp (with subtitles) echo 4. Download with yt-dlp (with English subtitles) echo 5. Download with yt-dlp (with captions) echo 6. Download with yt-dlp (audio only)

:: D. Get user input set /p choice=Enter your choice:

:: E. Process the user's choice if "%choice%"=="1" ( yt-dlp "%temp_file%" ) else if "%choice%"=="2" ( yt-dlp -f best "%temp_file%" ) else if "%choice%"=="3" ( yt-dlp --write-sub "%temp_file%" ) else if "%choice%"=="4" ( yt-dlp --sub-lang en "%temp_file%" ) else if "%choice%"=="5" ( yt-dlp -cit "%temp_file%" ) else if "%choice%"=="6" ( yt-dlp -x --audio-format mp3 "%temp_file%" ) else ( echo Invalid choice. Please try again. )

:: F. Pause the script so that you can see the output pause

:: G. Delete the temporary file :: del "%temp_file%"

:: H.Exit the script exit /B

I hope everyone who reads this, will test it on their own systems and OSes. The obvious advantages are that it is a virus free method of downloading a bunch of Youtube videos, without having to type a lot, or paste anything.

This give you six options to choose from when it comes to downloading Youtube with YT-DLP

Anyone can expand the menu to their own liking.

I pinned the batch file to the Task Bar, so it's always available.

This batch file should be tweaked some more, starting with step G should that stay blocked, or not? I am not sure that's necessary, because the batch file does work.

I just didn't wanna download some GUI, of which I would not know whether it had viruses or not.