2

So here's where I am at currently:

cls
@echo off
title youtube downloader
:downloader
echo youtuber downloader!!!
pause
cls
echo please put the link:
set /p site=
echo Do you want to download %site% as mp3?
set /p choice
if %choice%== yes goto mp3
if %choice%== no goto mp4

:mp4
@echo off
cls
C:\Users\Andly\Documents\youtube-dl.exe %site%
echo ok
pause
end

:mp3
@echo off
cls
C:\Users\Andly\Documents\youtube-dl %site% --audio-format mp3
echo lol ok
pause
end

I am pretty sure I can select the link in

echo please put the link:
set /p site=

^^^ However, it crashes right when I put a site!

Does anyone know what I am doing wrong?

Andly Kwan
  • 51
  • 7

2 Answers2

1

it would help to show examples of the pages you are attempting to download. Some youtube links contain Ampersands '&', which the command line interprets as indicating a new command on the same line. This is likely what is causing your code to fail. '&' Is not the only symbol that can disrupt the execution of your code.

For further clarification, here is an example of a site adress:

https://www.youtube.com/watch?v=8JEhYPOHBeU&list=PL3EtSYnnXk-rMpHWZ-B6gsDjiydt8JijL&index=2&t=0s

This is what happens when command tries to expand the variable site:

C:\Users\name\folder>ECHO https://www.youtube.com/watch?v=8JEhYPOHBeU  & 
list=PL3EtSYnnXk-rMpHWZ-B6gsDjiydt8JijL  & index=2  & t=0s
https://www.youtube.com/watch?v=8JEhYPOHBeU
'list' is not recognized as an internal or external command,
operable program or batch file.
'index' is not recognized as an internal or external command,
operable program or batch file.
't' is not recognized as an internal or external command,
operable program or batch file.

Wherever '&' is encountered in the variable when it's expanded, command tries to read what follows it as a new command.

T3RR0R
  • 1,830
  • 2
  • 7
  • 17
  • Can you explain to me what you mean, because I don't think I used the '&' symbol, and this is batch. I am using the executable youtube-dl, a command line app that let's you download youtube videos through command prompt(as I said in the title). I am trying to add youtube-dl's program to my command line, making it easier to use. – Andly Kwan Dec 20 '19 at 08:27
  • In the link you are using is where the issue likely lies given the timing you described. Youtube links / web adresses can contain Symbols such as '&' which can cause problems when set as part of a variable. It would be helpful if you showed examples of the site addresses you are using. – T3RR0R Dec 20 '19 at 08:38
  • Oh, i see what you mean. You do not need to put index or 0s unless you want a specific part, I am not making it so complex right now, maybe later on! – Andly Kwan Dec 20 '19 at 23:30
0

The majority of issues with potential poison characters in your input links, can be fixed by enclosing that input in doublequotes, as I already commented upon. Aside from that, a special case needs to account for the fact that in a batch file any % characters need to be doubled!

What follows is a simple example which should account for most of the issues anticipated with links containing poison characters:

@Echo Off
Title YouTube Downloader
SetLocal DisableDelayedExpansion
Echo YouTube Downloader!!!

:AskURI
Set "URI="
Set /P "URI=Please enter your YouTube link here>"
If Not Defined URI ClS & GoTo AskURI
Choice /M "Do you want to download as MP3"
If "%ErrorLevel%" == "1" (Call :GetDownload --audio-format mp3)Else Call :GetDownload
Echo Done
Pause
GoTo :EOF

:GetDownload
SetLocal EnableDelayedExpansion
"%UserProfile%\Documents\youtube-dl.exe" "!URI:%%=%%%%!" %*
Exit /B

As the example above uses choice.exe instead of the less robust, Set /P method, it bypasses an issue in your code, which used set /p choice instead of set /p choice=

Compo
  • 30,301
  • 4
  • 20
  • 32
  • Thanks! Adding the Set "thing=" Set /p "thing=question" worked! – Andly Kwan Dec 20 '19 at 23:27
  • However, I have begun a new thred-> https://stackoverflow.com/questions/59432184/how-to-download-mp3-using-youtube-dl-in-batch Please take a look, thank you! – Andly Kwan Dec 20 '19 at 23:29
  • @AndlyKwan, I've seen the new question, and all I could think is why use that, when the above is so much better. _Especially as you're only likely to need to change the `call` argument on line 11_. – Compo Dec 21 '19 at 00:32
  • OH! I just saw it and it was a redirect, so I used it so when there is something wrong, it goes back to before! – Andly Kwan Dec 22 '19 at 10:04