0

I have made a Youtube Downloader but unable to save the video according to the path input by the user ; below is my code . Kindly refer this , I have edited the code. I am trying to make a GUI YT video downloader and I have imported every pre requisite libraries and when I do in video.download(path) , I get an error class is not defined and when I add inverted commas to it it automatically creates a new folder named PATH

from tkinter import *
from pytube import YouTube
from tkinter import filedialog

root = Tk()
root.geometry('800x600')
root.resizable(0,0)
root.title("YouTube Video Downloader")
def browseFiles():
    path = filedialog.askdirectory()
    location = '"' + path + '"'
    print(location)


Label(root,text = 'Youtube Video Downloader', font ='arial 20 bold').pack()
Label(root , text = 'Choose directory' , font = 'arial 20 bold').place(x =120, y=190)

button_explore = Button(text = "Choose directory" , command = browseFiles)

link = StringVar()

Label(root, text = 'Paste your Link Here:', font = 'calibri 25 bold').place(x= 250 , y = 50)
link_enter = Entry(root, width = 90,textvariable = link).place(x = 120, y = 110)



#syntax to download video from youtube


def Downloader():
     
    link1 =YouTube(str(link.get()))
    video = link1.streams.filter(res = "1080p").first()
    video.download(r'path')
    Label(root, text = 'DOWNLOADED', font = 'calibri 20').place(x= 180 , y = 210)  


Button(root,text = 'DOWNLOAD', font = 'calibri 15 bold' ,bg = 'firebrick1', padx = 2, command = Downloader).place(x=300 ,y =350)
button_exit = Button(text = "Exit" , command = exit)
button_exit.place(x= 250,y=500)
button_explore.place(x = 120, y= 240)

root.mainloop()

1 Answers1

0

Without more information its tough to know exactly why, but I used to have a similar problem and I fixed it by writing my path like save(r'path')

Julian Avila
  • 105
  • 10
  • can you please briefly explain it – ALPHA HOTSHOT May 14 '21 at 19:29
  • The 'r' is a string prefix, which means the string that follows is a raw string. You need this so that windows reads the backslashes in your path. The backslash is otherwise an 'escaped character' So I would try writing, video.download(r'path') – Julian Avila May 14 '21 at 19:34
  • The [Python Documentation](https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals) does a good job of explaining what these string prefixes do. – Julian Avila May 14 '21 at 19:37
  • hello Julian , I have added my full code , u can refer to it thanks for your help , – ALPHA HOTSHOT May 14 '21 at 19:44
  • using 'r' prefix doesnt work it just created a new folder named path – ALPHA HOTSHOT May 14 '21 at 19:47
  • Where you write `video.download(r'path')`, python can't read what the selected path is in a string, and path isn't a stored variable. Have the user select the path and save it as a variable 'path'. Then use the format function ie. `video.download(r'{}'.format(path))` That is how I would go by doing it. – Julian Avila May 14 '21 at 20:06
  • If that doesn't work checkout this [web page](https://stackoverflow.com/questions/40713268/download-youtube-video-using-python-to-a-certain-directory/54163735) – Julian Avila May 14 '21 at 20:09
  • can you please write the exact code , as what ur saying about the user select the path and save it as a variable path like that please – ALPHA HOTSHOT May 14 '21 at 20:32
  • video.download(r'{}'.format(location)) NameError: name 'location' is not defined getting this error – ALPHA HOTSHOT May 14 '21 at 20:51
  • In your `def browseFiles():`, you save the path to a variable 'location'. You would have to call that variable like `video.download(r' + location + ')` or `video.download(r'{}'.format(location))` (I am not totally sure on the syntax). – Julian Avila May 14 '21 at 21:19
  • I would write the code but I don't know to work with that function. The problem you are having is that you are letting the user select the path in the directory, but then you aren't calling the actual path when you save, you are just saving it in a folder 'path'. You need to call the variable, but when I try to call location it doesn't recognize the variable outside of the function. If you write `video.downloader(r'C:\Users\User\Desktop')` it saves the video to your desktop, So everything else works. You just need to find a way to adjust your function so that you can call the path variable. – Julian Avila May 14 '21 at 21:23