错误和延迟从Youtube视频下载器与Python

xqkwcwgp  于 2023-01-06  发布在  Python
关注(0)|答案(1)|浏览(165)

我可以下载视频,但它没有显示字符串“下载完成”,视频也没有出现在我选择的路径中,而是出现在我保存代码的文件夹中。此外,控制台显示错误,下载视频需要一段时间。
目的很简单,你粘贴一个YouTube视频的链接,然后选择一个你想要下载视频的本地路径。

from tkinter import *
from tkinter import filedialog
from moviepy import *
from moviepy.editor import VideoFileClip
from pytube import YouTube
import shutil

screen = Tk()
title = screen.title("Youtube Downloader")
canvas = Canvas(screen, width=500, height=500)
canvas.pack()

def select_path():
    path = filedialog.askdirectory()
    path_label.config()

def download_video():
    get_link = link_field.get()
    user_path = path_label.cget("text")
    screen.title('Downloading... Please wait...')
    mp4_video = YouTube(get_link).streams.get_highest_resolution().download()
    vid_clip = VideoFileClip(mp4_video)
    vid_clip.close()
    shutil.move(mp4_video, user_path)
    screen.title('Download Completed!')

logoimg = PhotoImage(file="images/download.png")
logoimg = logoimg.subsample(2, 2)
canvas.create_image(250, 80, image=logoimg)

link_field = Entry(screen, width=50)
link_label = Label(screen, text="Paste Download Link: ", font=("Sans Serif", 13))

canvas.create_window(250, 170, window=link_label)
canvas.create_window(250, 210, window=link_field)

path_label = Label(screen, text="Select Local Path: ", font=("Sans Serif", 13))
select_button = Button(screen, text="Select", command=select_path)    # Select Button

canvas.create_window(250, 270, window=path_label)
canvas.create_window(250, 320, window=select_button)

download_bttn = Button(screen, text="Download Video", font=("Sans Serif", 12),  command=download_video)
canvas.create_window(250, 400, window=download_bttn)

screen.mainloop()
e37o9pze

e37o9pze1#

看起来你实际上并没有更新你的path_label文本。你在select_path()中调用path_label.config(),但是你没有传递任何东西给config调用。我假设你想要path_label.config(text=path)

  • 然而 *-这是处理这类事情的一种非正统的方式。你可能会更容易地将一个StringVar()绑定到你的path_labeltextvariable上。这样,每当StringVar()的值改变时(通过调用StringVar.set()),标签就会自动更新。你也可以很容易地用StringVar.get()检索值
path_var = StringVar("Select Local Path: ")
path_label = Label(screen, textvariable=path_var, font=("Sans Serif", 13))
def select_path():
    # update the variable bound to path_label
    path_var.set(filedialog.askdirectory())
# fetch the current value of 'path_var'
user_path = path_var.get()

相关问题