WXPython becoming unresponsive during youtube download with Pytube

Viewed 78

I Am New In wxpython. I am Making Program using the pytube library. I can download youtube videos just fine on its own, but when I put it in the WXPython method, the UI suddenly hangs and becomes unresponsive. But When The Video finnished downloading, the UI is responsive again. This Is My Code.

import wx
import pytube
from pytube import YouTube
from pytube import Playlist
import os
import time
from threading import Timer as wait
from accessible_output2 import outputs
say=outputs.auto.Auto()
speak=say.speak
app = wx.App()
class abhishek(wx.Frame):
    def __init__(self):
        super().__init__(None, -1, title = 'ar-developers YouTube Downloader, 1.2.')
        self.p = wx.Panel(self)
        self.Center()
        vid = wx.Button(self.p, -1, 'Download Video...')
        vid.Bind(wx.EVT_BUTTON, self.video_download)
        aud = wx.Button(self.p, -1, 'Download Audio...')
        aud.Bind(wx.EVT_BUTTON, self.audio_download)
        pv = wx.Button(self.p, -1, 'Download Playlist...')
        pv.Bind(wx.EVT_BUTTON, self.playlist_download)
        pa = wx.Button(self.p, -1, 'Download Playlist in Audio Format...')
        pa.Bind(wx.EVT_BUTTON, self.playlist_audio_download)
        self.Show()
    def on_progress(self, stream=None, chunk=None, bytes_remaining=None):
        self.x = '%'
        self.total_size = self.video.filesize
        self.bytes_downloaded = self.total_size - bytes_remaining
        self.liveprogress = int(self.bytes_downloaded / self.total_size * 100)
        speak(f"downloading, {self.liveprogress} {self.x}")
        time.sleep(2)
    def video_download(self, event):
        url = wx.GetTextFromUser('Type the URL of your video', 'URL')
        try:
            youtube = pytube.YouTube(url,on_progress_callback=self.on_progress)
        except:
            wx.MessageBox('Please enter a valid URL', 'Alert')
            exit()
        speak(f'downloading, {youtube.title}')
        self.video = youtube.streams.get_highest_resolution()
        self.video.download("Downloads")
        wx.MessageBox('Download Complete!', 'Complete')
    def audio_download(self, event):
        url = wx.GetTextFromUser("Enter URL of Your Video", "URL")
        try:
            divi = YouTube(url, on_progress_callback=self.on_progress)
        except:
            wx.MessageBox('Please Enter a Valid URL','Error')
            exit()
        speak(f'downloading, {divi.title}')
        self.video = divi.streams.filter(only_audio=True).first()
        self.out_file = self.video.download("Downloads")
        base, ext = os.path.splitext(self.out_file)
        self.new_file = base + '.mp3'
        os.rename(self.out_file, self.new_file)
        wx.MessageBox('Download Complete!', 'Complete')
    def playlist_download(self, event):
        url = wx.GetTextFromUser("Enter the URL of your playlist. ", "URL")
        try:
            playlist = Playlist(url)
        except:
            wx.MessageBox('Please Enter a Valid URL','Error')
            exit()
        speak('Please Wait, While We Are Downloading Your Play List')
        for p in playlist.video_urls:
            f = pytube.YouTube(p,on_progress_callback=self.on_progress)
            speak(f'downloading, {f.title}')
            self.video = f.streams.get_highest_resolution()
            self.video.download("Downloads")
    def playlist_audio_download(self, event):
        url = wx.GetTextFromUser("Enter URL of your playlist", "URL")
        try:
            playlist = Playlist(url)
        except:
            wx.MessageBox('Please Enter a Valid URL','Error')
            exit()
        speak('Please Wait, While We Are Downloading Your Play List')
        for p in playlist.video_urls:
            f = YouTube(p, on_progress_callback=self.on_progress)
            speak(f'downloading, {f.title}')
            self.video = f.streams.filter(only_audio=True).first()
            self.out_file = self.video.download("Downloads")
            base, ext = os.path.splitext(self.out_file)
            self.new_file = base + '.mp3'
            os.rename(self.out_file, self.new_file)
abhishek()
app.MainLoop()
1 Answers

It is a general issue. Not only in wx the not responding issue is happening in tkinter too. But it happens when you run the code after compiling to an exe file.

Related