Search code examples
windowsbatch-fileyoutube-dl

Windows Batch: using user input as command argument


I recently discovered youtube-dl and I wanted to make a batch file within the folder of the youtube-dl executable that asks for a url and uses that url in the

youtube-dl --extract-audio --audio-format mp3 -i -o songs\%(title)s.%(ext)s <video URL>

command, which saves the video as an mp3 file in the \songs\ folder in the same filepath as the executable. The command works fine if I copy-paste it into a command prompt and replace <video url> with the actual url, but when I try to put this in a batch file (e.g. with set var1="") it uses the variable's name in the command (youtube-dl --extract-audio --audio-format mp3 -i -o songs\%(title)s.%(ext)s var1).

I've found this, and it seems to be what I'm looking for, but it didn't make any sense.


Solution

  • When copying the command into a batch file, you'll need to replace % with %%, so that the output template parameter to the -o option will be interpreted correctly (i.e. as the string %(title)s.%(ext)s).

    If you prefer to be prompted to enter the link, batch file may look like this:

    set /p var1=  "Enter youtube link: "
    youtube-dl --extract-audio --audio-format mp3 -i -o songs\%%(title)s.%%(ext)s %var1%
    

    Or, just set var1 before calling the script (set var1=youtube-link), or in the script itself.