YouTube视频下载播放程序的Pafy组件报错求助
解决YouTube下载器的两类异常:TypeError与AttributeError
错误日志
Exception in thread Thread-4: Traceback (most recent call last): File "C:\Users\ibraheem\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner self.run() File "C:\Users\ibraheem\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "C:\Users\ibraheem\Downloads\YoutubeDownloader\source\gui\activity_dialog.py", line 34, in run raise e File "C:\Users\ibraheem\Downloads\YoutubeDownloader\source\gui\activity_dialog.py", line 30, in run self.res = self.function(*self.args, **self.kwargs) File "C:\Users\ibraheem\Downloads\YoutubeDownloader\source\utiles.py", line 27, in get_video_stream for stream in media.streams: File "C:\Users\ibraheem\Downloads\YoutubeDownloader\source\pafy\backend_shared.py", line 160, in streams self._process_streams() File "C:\Users\ibraheem\Downloads\YoutubeDownloader\source\pafy\backend_youtube_dl.py", line 83, in _process_streams allstreams = [YtdlStream(z, self) for z in self._ydl_info['formats']] Traceback (most recent call last): File "C:\Users\ibraheem\Downloads\YoutubeDownloader\source\pafy\backend_youtube_dl.py", line 83, in <listcomp> File "C:\Users\ibraheem\Downloads\YoutubeDownloader\source\youtube_browser\browser.py", line 182, in <lambda> allstreams = [YtdlStream(z, self) for z in self._ydl_info['formats']] File "C:\Users\ibraheem\Downloads\YoutubeDownloader\source\pafy\backend_youtube_dl.py", line 107, in __init__ self.searchResults.Bind(wx.EVT_MENU, lambda event: self.playVideo(), id=self.videoPlayItemId) self._rawbitrate = info.get('abr', 0) * 1024 File "C:\Users\ibraheem\Downloads\YoutubeDownloader\source\youtube_browser\browser.py", line 134, in playVideo TypeError: unsupported operand type(s) for *: 'NoneType' and 'int' stream = LoadingDialog(self, _("starting..."), get_video_stream, url).res AttributeError: 'LoadingDialog' object has no attribute 'res'
1. 修复TypeError:None与int相乘错误
- 问题根源:
pafy/backend_youtube_dl.py第107行中,info.get('abr', 0)返回了None(YouTube流信息未提供音频比特率字段),尝试与整数1024相乘触发类型错误。 - 修复代码:
打开C:\Users\ibraheem\Downloads\YoutubeDownloader\source\pafy\backend_youtube_dl.py,找到第107行:
修改为:self._rawbitrate = info.get('abr', 0) * 1024
此修改确保当self._rawbitrate = (info.get('abr') or 0) * 1024abr为None时,自动用0替代,避免类型不兼容问题。
2. 修复AttributeError:LoadingDialog无res属性错误
- 问题根源:
get_video_stream执行抛出TypeError时,LoadingDialog的run方法中self.res = self.function(...)未执行,导致res属性未初始化,后续访问时触发错误。 - 修复方案:
方案一:先修复上述TypeError,确保get_video_stream正常执行,res属性会被正确赋值。
方案二:修改youtube_browser/browser.py第134行代码,增加异常捕获:
方案三:修改dialog = LoadingDialog(self, _("starting..."), get_video_stream, url) try: stream = dialog.res except AttributeError: wx.MessageBox(_("视频流加载失败,请检查网络或视频链接"), _("错误提示")) returnLoadingDialog类(位于gui/activity_dialog.py),在__init__方法中初始化self.res = None,确保属性始终存在:def __init__(self, parent, message, function, *args, **kwargs): # 原有初始化代码 self.res = None # 其他代码
内容的提问来源于stack exchange,提问作者ibraheem mohsen
相关产品推荐
相关产品推荐

