如何解决在使用 Pytube 下载 youtube 期间,WXPython 变得无响应
我是 wxpython 的新手。 我正在使用 pytube 库制作程序。 我可以自己下载youtube视频就好了,但是当我将其放入WXPython方法中时,UI突然挂起并且没有响应。但是当视频完成下载时,用户界面再次响应。 这是我的代码。
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,'Download Video...')
vid.Bind(wx.EVT_BUTTON,self.video_download)
aud = wx.Button(self.p,'Download Audio...')
aud.Bind(wx.EVT_BUTTON,self.audio_download)
pv = wx.Button(self.p,'Download Playlist...')
pv.Bind(wx.EVT_BUTTON,self.playlist_download)
pa = wx.Button(self.p,'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",While We Are Downloading Your Play List')
for p in playlist.video_urls:
f = YouTube(p,{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()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。