I have python application with pytube and tkinter

Question:

i made an application, where you can download a youtube video and choose the location of download. The problem is, it only work sometimes.

This is the error it is giving in console: Exception in Tkinter callback Traceback (most recent call last): File "C:UsersUserAppDataLocalProgramsPythonPython39libtkinter__init__.py", line 1885, in __call__ return self.func(*args) File "C:PythonVideoDownloaderVideoDownloader.py", line 39, in downloadVideo ytbvideo=YouTube(ytbLink).streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first() File "C:UsersUserAppDataLocalProgramsPythonPython39libsite-packagespytube__main__.py", line 91, in __init__ self.prefetch() File "C:UsersUserAppDataLocalProgramsPythonPython39libsite-packagespytube__main__.py", line 183, in prefetch self.js_url = extract.js_url(self.watch_html) File "C:UsersUserAppDataLocalProgramsPythonPython39libsite-packagespytubeextract.py", line 143, in js_url base_js = get_ytplayer_config(html)["assets"]["js"] KeyError: 'assets'

And this is my code:

import os
from tkinter import *
from tkinter import filedialog
import tkinter as tk



root= Tk()
root.geometry('600x400')
root.title('Youtube Video Downloader')
root.configure(bg='gray')



Label_1=Label(root,text="Youtube video downloader", font=("bold",20), bg='gray')
Label_1.place(x=150,y=10)

Label_2=Label(root, text="Paste the link here", font=(10), bg='gray')
Label_2.place(x=240, y=75)



mylink=StringVar()

pastelink=Entry(root, width=60, textvariable=mylink)
pastelink.place(x=140, y=100)



def chooseDir():
    global path
    path = filedialog.askdirectory(title="Choose a download directory")
    videoLoc = path
    tk.Label(root, text=path, bg='gray').place(x=240,y=300)

def downloadVideo():
    ytbLink=str(mylink.get())
    ytbvideo=YouTube(ytbLink).streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
    ytbvideo.download(videoLoc)


    
def quitApp():
    root.destroy()
    



   

Button(root,text="Download video", width=20, bg='black',fg="white", command=downloadVideo).place(x=240, y=130)
Button(root,text="Choose location", width=20, bg='black',fg="white", command=chooseDir).place(x=240, y=160)
Label_3=Label(root, text="Curent location: ", font=("bold"), bg='gray')
Label_3.place(x=250, y=245)

Label_3=Label(root, text="by yakubiq", font=("bold"), bg='gray')
Label_3.place(x=0, y=375)

Button(root,text="Quit", width=20, bg='black', fg='white', command=quitApp).place(x=445, y=370)



root.mainloop()

Im trying to fix it on my own, but still, im beginner and I started few days ago. In console, there is line 183, but i dont know, what does it mean

Asked By: yakubiq

||

Answers:

This issue is fixed on the newer version of pytube, just try to say pip uninstall pytube and then pip install pytube and then re-run your code again.

And as far as your tkinter code is concerned, you are assigning global to the wrong variable.

def chooseDir():
    global videoLoc #correct globalization
    path = filedialog.askdirectory(title="Choose a download directory")
    videoLoc = path
    tk.Label(root, text=path, bg='gray').place(x=240,y=300)

def downloadVideo():
    ytbLink = mylink.get() #no need to use str() as it is string by default 
    ytbvideo = YouTube(ytbLink).streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
    ytbvideo.download(videoLoc)

With these changes it should work, but keep in mind, in the example you gave, your not importing pytube, so it has to be like from pytube import YouTube. Though i would also suggest you use threading.

Answered By: Delrius Euphoria

you forgot to import pytube

from pytube import Youtube

and the ytbvideo.download(videoLoc)

videoLoc is not defined

Answered By: REfil
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.