0
from tkinter import *
from tkinter import filedialog
from tkinter.ttk import Progressbar

import pytubefix.exceptions
from pytubefix import YouTube
import time

def openFile():
    global filepath
    filepath = filedialog.askdirectory()
    print(filepath)

def info(stream, chunk, bytes_remaining):
    print('run')
    total_size = stream.filesize
    bytes_downloaded = total_size - bytes_remaining

    percentage = (bytes_downloaded / total_size) * 100

    if bar['value'] < 100:
        bar['value'] = percentage
        window.update_idletasks()
    else:
        bar['value'] = 100
        print("Done!")

#setup
window = Tk()
window.geometry("400x400")
window.title("Jun")
window.config(background='black')

label = Label(window, text='Youtube Video Downloader')
label.config(font=('Monospace', 22))
label.pack()

button = Button(window, text="Click me to select a directory!", command=openFile)
button.config(font=('Monospace',10))
button.pack()

entry = Entry()
entry.config(font=('Monospace', 20))
entry.insert(0, 'Enter link here')

bar = Progressbar(window, orient=HORIZONTAL, length=300)
bar.pack(pady=10)

def submit():
    url = entry.get()
    if not url or url == 'Enter link here':
        print("Error: Please enter a valid YouTube link.")
    try:
        print("try block running")
        yt = YouTube(url, on_progress_callback=info)
        print(f"Video Name: {yt.title}")

        ys = yt.streams.get_highest_resolution()
        ys.download(output_path=filepath)
    except NameError:
        print("Enter a valid directory")
    except pytubefix.exceptions.VideoUnavailable:
        print("Video is unavailable")
download = Button(window, text="submit", command=submit)

entry.pack()
download.pack()

window.mainloop()

I'm trying to make a youtube downloader gui with Pytubefix, but it seems like it only works occasionally. In my code it only prints the title of the video without printing any of the other indicators i've added that would show the download is commencing. All it prints is "try block running Video Name: (video name)

Process finished with exit code 0"

1
  • issue still persists Commented Nov 9 at 4:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.