Python 如何在我下载视频时从 youtube-dl 获取下载百分比
Python how do I get the download percentage from youtube-dl when im downloading a video
我想获取 python 的 youtube-dl 模块的下载百分比,原因是我想将它放入 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 分享了一个 link 答案,但是当你想把它放入 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'])
我想获取 python 的 youtube-dl 模块的下载百分比,原因是我想将它放入 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 分享了一个 link 答案,但是当你想把它放入 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'])