python - Python在我下载视频时如何从youtube-dl获取下载百分比
问题描述
我想从 youtube-dl 模块获取 python 的下载百分比,原因是我想把它放到 pyqt5 的进度条中。我在 youtube-dl 的 GitHub 或其他任何地方都没有找到和文档,有人碰巧知道吗?
我在 google/stack overflow/GitHub 上的任何地方都没有找到任何成功的答案。
def downloadYoutube(self):
self.changeText()
# try:
self.lblState.setText('Downloading...')
url = self.txtURL.text()
if 'https://www.youtube.com/watch?' not in url:
buttonReply = QMessageBox.critical(self, 'Error! :(', "{} is an invalid URL".format(url), QMessageBox.Ok, QMessageBox.Ok)
return
# if 'https://youtu.be/' not in url:
# buttonReply = QMessageBox.critical(self, 'Error! :(', "{} is an invalid URL".format(url), QMessageBox.Ok, QMessageBox.Ok)
# return
if self.radAudio.isChecked() == True:
ydl_opts = {
'format': 'bestaudio/best',
'extractaudio': True,
'audioformat': "mp3",
'noplaylist': True,
}
else:
ydl_opts = {
'noplaylist': True,
}
info_dict = youtube_dl.YoutubeDL(ydl_opts).extract_info(url, download = False)
video_id = info_dict.get("id", None)
video_title = info_dict.get('title', None)
self.lblTitle.setText(str(video_title))
try:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
self.lblState.setText('Downloading...')
ydl.download([url])
except Exception as e:
self.lblState.setText('')
buttonReply = QMessageBox.critical(self, 'Error! :(', "Problem downloading {}\n\nError Log:\n{}".format(url, e), QMessageBox.Ok, QMessageBox.Ok)
return
我希望百分比以可变形式出现,但我似乎无法从任何地方提取百分比。
解决方案
我将在这里分享答案,因为Marceline分享了答案的链接,但是当您想将其放入 pyqt5 进度条时,我会在这里分享...
ydl_opts = {
'format': 'bestaudio/best',
'extractaudio': True,
'audioformat': "mp3",
'progress_hooks': [self.my_hook],
'noplaylist': True
}
def my_hook(self, d):
if d['status'] == 'finished':
file_tuple = os.path.split(os.path.abspath(d['filename']))
print("Done downloading {}".format(file_tuple[1]))
if d['status'] == 'downloading':
p = d['_percent_str']
p = p.replace('%','')
self.progress.setValue(float(p))
print(d['filename'], d['_percent_str'], d['_eta_str'])
推荐阅读
- android - 数据绑定在动态功能模块中不起作用,在引用绑定的视图时导致空指针异常
- c - 编译 HPC Challenge Benchmark 时出现未声明的变量错误
- html - 如何在多个文件条件下设置文件上传限制
- apache-spark - “应用程序尝试...在 ApplicationMasterService 缓存中不存在”原因?(Pregel:非收敛算法对集群的 maxIterations 影响)
- optimization - AnyLogic:有没有办法在优化实验中指定决策变量数组?
- php - 将数学表达式拆分为数组,而不在括号和单引号之间拆分子表达式
- powershell - Powershell 在远程计算机上使用命令行参数执行 exe
- python - Python 3.X | 确定坐标是否位于矩形内
- c# - .Net Core 依赖注入:获取服务类型(接口)的实现类型
- javascript - REACT NATIVE:如何通过调用 API 从 JSON 中插入所有图片