Home > database >  Error and delay from Youtube Video Downloader made with Python
Error and delay from Youtube Video Downloader made with Python

Time:01-03

I can download the video, but it doesn't display the string "Download Completed" and the video doesn't appears in the path I selected, instead it appears in the folder that I saved my code. Also, the console shows me an error and it takes a while to download the video.

The purpose is very simple, you paste a link of a YouTube video and select a local path that you want your video to be downloaded.

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

CodePudding user response:

It looks like you aren't actually updating your path_label text. You call path_label.config() in select_path(), but you aren't passing anything to the config call. I assume you want path_label.config(text=path).

However - this is an unorthodox way of handling this sort of thing. You may have an easier time binding a StringVar() to your path_label's textvariable. That way, whenever the value of the StringVar() changes (via a call to StringVar.set()), the label is updated automatically. You can also easily retrieve the value with 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()
  • Related