[Python] Python YouTube downloader

Discussão em 'Python' iniciado por Stack, Junho 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to build a Youtube downloader app using Python. When I put link on the field - appears this issue

    Exception in Tkinter callback
    Traceback (most recent call last):
    File "C:\Users\truemaxevans\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
    File "C:/Users/truemaxevans/PycharmProjects/pythonProject25/main.py", line 57, in __get_link
    self.downloader(link, path, name, ext)
    File "C:/Users/truemaxevans/PycharmProjects/pythonProject25/main.py", line 45, in downloader
    yt = YouTube(link)
    File "C:\Users\truemaxevans\PycharmProjects\pythonProject25\venv\lib\site-packages\pytube\__main__.py", line 73, in __init__
    self.video_id = extract.video_id(url)
    File "C:\Users\truemaxevans\PycharmProjects\pythonProject25\venv\lib\site-packages\pytube\extract.py", line 162, in video_id
    return regex_search(r"(?:v=|\/)([0-9A-Za-z_-]{11}).*", url, group=1)
    File "C:\Users\truemaxevans\PycharmProjects\pythonProject25\venv\lib\site-packages\pytube\helpers.py", line 129, in regex_search
    raise RegexMatchError(caller="regex_search", pattern=pattern)
    pytube.exceptions.RegexMatchError: regex_search: could not find match for (?:v=|\/)([0-9A-Za-z_-]{11}).*

    Process finished with exit code 0


    Using tkinter and pytube for this project, I cant find this issue in official documentation and google. Here is a code:

    from pytube import YouTube
    import tkinter as tk

    WINDOW_WIDTH = 500
    WINDOW_HEIGHT = 200
    WINDOW_TITLE = 'YouTube downloader'
    BUTTON_CLICK_SOUND = 'play'


    class YouTubeDownloader:

    def __init__(self):
    self.window = tk.Tk()
    self.window.geometry("{}x{}".format(WINDOW_WIDTH, WINDOW_HEIGHT))
    self.window.configure(bg='#a9dcd6')
    self.window.title(WINDOW_TITLE)

    # Create labels
    self.link_lable = tk.Label(self.window, text='Download link')
    self.link_lable.grid(column=0, row=0)
    self.link_lable = tk.Label(self.window, text='Save file as')
    self.link_lable.grid(column=0, row=1)
    self.link_lable = tk.Label(self.window, text='Save PATH')
    self.link_lable.grid(column=0, row=2)
    self.link_lable = tk.Label(self.window, text='File extension')
    self.link_lable.grid(column=0, row=3)

    # Create entry fields
    self.link_entry = tk.Entry(master=self.window, width=60)
    self.link_entry.grid(column=1, row=0)
    self.link_entry = tk.Entry(master=self.window, width=60)
    self.link_entry.grid(column=1, row=1)
    self.link_entry = tk.Entry(master=self.window, width=60)
    self.link_entry.grid(column=1, row=2)
    self.link_entry = tk.Entry(master=self.window, width=60)
    self.link_entry.grid(column=1, row=3)

    # Download button
    self.download_button = tk.Button(self.window, text='Submit', command=self.__get_link)
    self.download_button.grid(column=1, row=4)

    return

    def downloader(self, link, save_path='', save_name='', extension='mp4'):
    yt = YouTube(link)
    yt.streams.filter(progressive=True, file_extension=extension).order_by('resolution').desc().first()
    yt.streams.download(output_path=save_path, filename=save_name)

    return

    def __get_link(self):
    link = self.link_entry.get()
    path = self.link_entry.get()
    name = self.link_entry.get()
    ext = self.link_entry.get()

    self.downloader(link, path, name, ext)

    return

    def run_app(self):
    self.window.mainloop()
    return


    if __name__ == '__main__':
    app = YouTubeDownloader()
    app.run_app()

    Continue reading...

Compartilhe esta Página