0

我做了一个应用程序,您可以在其中下载 youtube 视频并选择下载位置。问题是,它只在某些时候起作用。

这是它在控制台中给出的错误:Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1885, in __call__ return self.func(*args) File "C:\Python\VideoDownloader\VideoDownloader.py", line 39, in downloadVideo ytbvideo=YouTube(ytbLink).streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first() File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 91, in __init__ self.prefetch() File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\__main__.py", line 183, in prefetch self.js_url = extract.js_url(self.watch_html) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pytube\extract.py", line 143, in js_url base_js = get_ytplayer_config(html)["assets"]["js"] KeyError: 'assets'

这是我的代码:

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()

我试图自己修复它,但我仍然是初学者,我几天前就开始了。在控制台中,有第 183 行,但我不知道,这是什么意思

4

1 回答 1

1

此问题已在较新版本的 中修复pytube,只需尝试说出pip uninstall pytube然后pip install pytube重新运行您的代码即可。

就您的 tkinter 代码而言,您分配global给了错误的变量。

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)

通过这些更改,它应该可以工作,但请记住,在您给出的示例中,您没有导入 pytube,所以它必须像from pytube import YouTube. 虽然我也建议你使用threading.

于 2020-11-20T16:27:42.393 回答