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```
7
  • 1
    the clip command appears to be to put text on the clipboard rather than copying data from it. Where did you get the syntax for it? If I run clip /? then the only description is Redirects output of command line tools to the Windows clipboard. not Redirect clipboard to command line
    – Mokubai
    Commented Oct 25, 2023 at 16:38
  • yes, you're right. please see the above EDIT. Commented Oct 25, 2023 at 16:57
  • Your first set line is also incorrect. Its missing 2 "'s between the =.
    – LPChip
    Commented Oct 25, 2023 at 17:05
  • powershell get-clipboard > 1.txt see How to Access the System Clipboard (Copy/Paste) from the Command Line | by Zack | Level Up Coding
    – DavidPostill
    Commented Oct 25, 2023 at 18:11
  • @lpchip like this? set "temp_file"="%TEMP%\%RANDOM%" ? Because that produces the error: The system cannot find the path specified. \ Please select an option: \ 1. Download with yt-dlp \ 2. 3. 4. 5. \ . \ 6. Download with yt-dlp (audio) \ Enter your choice:1 \ [generic] Extracting URL: \ ERROR: [generic] '' is not a valid URL. Set --default-search "ytsearch" (or run yt-dlp "ytsearch:" ) to search YouTube \ Press any key to continue... \ C:\Programs\*, Are you sure (Y/N)? n \ Commented Oct 25, 2023 at 21:55

1 Answer 1

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 "Get-Clipboard"`) 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .