【发布时间】:2021-03-19 10:01:21
【问题描述】:
我先给出我的代码:
import youtube_dl
from tkinter import *
from tkinter.messagebox import showinfo, showerror
from tkinter.filedialog import askdirectory
import pyglet
import validators
pyglet.font.add_file('BREAKBONE.ttf')
ytvdw = Tk()
download_path = StringVar()
video_link = StringVar()
ydl_opts = {'format': 'bestaudio/best',
'quiet': False,
'extractaudio': True, # only keep the audio
'audioformat': "mp4", # convert to wav
'outtmpl': '%(id)s.mp4', # name the file the ID of the video
'noplaylist': True, # only download single song, not playlist
}
ytvdw.iconbitmap('swisstoollogo.ico')
ytvdw.title('YouTube Video Downloader - Swiss Tool Box')
ytvdw.geometry('500x500')
ytvdw.resizable(False, False)
heading = Label(ytvdw, text='YouTube Video Downloader', font=('BREAKBONE', 24, 'underline'))
heading.place(x=75, y=1)
info = Label(ytvdw,
text='This program is used to download any YouTube Video \n you want to download. Just paste the '
'link in the box \n below then click on the start button', font=('BREAKBONE', 15))
info.place(x=5, y=50)
Label(ytvdw, text='Link', font=('BREAKBONE',20)).place(x=210, y=150)
link_entry = Entry(ytvdw, width=60, textvariable=video_link)
link_entry.place(x=25, y=185)
Label(ytvdw, text='Directory', font=('BREAKBONE', 20)).place(x=180, y=230)
dir_entry = Entry(ytvdw, width=60, textvariable=download_path)
dir_entry.place(x=25, y=265)
def link_check():
link_check_var = str(link_entry.get())
validated_link = validators.url(link_check_var)
if validated_link is True:
showinfo('Link Check', 'Sucessful! Your link is valid. You may continue!')
elif validated_link is not False:
showerror('Link Check', 'Sucessful! You link is not valid. Please check the link again!')
else:
showerror('Link Check', 'Some unexpected error occured. Please re-run the program.')
def browse_directory():
download_directory = askdirectory(initialdir='C:/Users/Bhavyadeep/Desktop/YT Vid Download')
download_path.set(download_directory)
def download_video():
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([zxt])
showinfo('Video Downloaded', 'Your video has been downloaded in:'+ download_path)
yt_link = video_link.get()
zxt = yt_link.strip()
link_check_button = Button(ytvdw, text='Check!', command=link_check)
link_check_button.place(x=400, y=182)
browse_button = Button(ytvdw, text='Browse!', command=browse_directory)
browse_button.place(x=400, y=262)
start_button = Button(ytvdw, text='Download!', command=download_video)
start_button.place(x=200, y=300)
ytvdw.mainloop()
我在使用 youtube_dl 模块时遇到错误。
最后的错误:
youtube_dl.utils.DownloadError: ERROR: '' is not a valid URL. Set --default-search "ytsearch" (or run youtube-dl "ytsearch:" ) to search YouTube
我检查后链接有效,并且运行良好。但是在添加链接之后,我得到了那个错误,好像没有包含链接的任何部分。
以防窗口冻结和崩溃以调试模式运行程序。
字体网址如下: https://www.dafont.com/breakbone.font
我对 youtube_dl 很陌生,还没有使用过它。请帮帮我!
【问题讨论】:
标签: python python-3.x youtube youtube-dl python-3.9