有没有办法在pytube中添加进度条?我不知道如何使用以下方法:
pytube.Stream().on_progress(chunk, file_handler, bytes_remaining)
我的代码:
from pytube import YouTube
# from pytube import Stream
from general import append_to_file
def downloader(video_link, down_dir=None):
try:
tube = YouTube(video_link)
title = tube.title
print("Now downloading, " + str(title))
video = tube.streams.filter(progressive=True, file_extension='mp4').first()
print('FileSize : ' + str(round(video.filesize/(1024*1024))) + 'MB')
# print(tube.streams.filter(progressive=True, file_extension='mp4').first())
# Stream(video).on_progress()
if down_dir is not None:
video.download(down_dir)
else:
video.download()
print("Download complete, " + str(title))
caption = tube.captions.get_by_language_code('en')
if caption is not None:
subtitle = caption.generate_srt_captions()
open(title + '.srt', 'w').write(subtitle)
except Exception as e:
print("ErrorDownloadVideo | " + str(video_link))
append_to_file('debug', format(e))
# FILESIZE print(tube.streams.filter(progressive=True, file_extension='mp4').first().filesize/(1024*1024))
答案 0 :(得分:3)
在Youtube类中调用您的进度函数
yt = YouTube(video_link, on_progress_callback=progress_func)
这是您的进度功能
def progress_function(self,stream, chunk,file_handle, bytes_remaining):
size = video.filesize
p = 0
while p <= 100:
progress = p
print str(p)+'%'
p = percent(bytes_remaining, size)
这会计算转换文件大小和剩余字节数的百分比
def percent(self, tem, total):
perc = (float(tem) / float(total)) * float(100)
return perc
答案 1 :(得分:2)
我知道已经回答了这个问题,但是我碰到了这个问题,对我来说进度是从100倒数到0。由于我想用百分比值填充进度条,所以我不能使用它。
所以我想出了这个解决方案:
def progress_func(self, stream, chunk, file_handle,bytes_remaining):
size = self.video.filesize
progress = (float(abs(bytes_remaining-size)/size))*float(100)
self.loadbar.setValue(progress)
负载栏是我从PyQt5开始的进度栏。 希望这对某人有帮助。
答案 2 :(得分:2)
较短的选项:
yt = YouTube(video_link, on_progress_callback=progress_function)
video = yt.streams.first() # or whatever
# Prints something like "15.555% done..."
def progress_function(stream, chunk, file_handle, bytes_remaining):
print(round((1-bytes_remaining/video.filesize)*100, 3), '% done...')
当然,您可以将进度输出限制为例如10%,20%,30%...的值-只需将print
语句括在必需的if
子句中即可。
答案 3 :(得分:1)
回调函数采用三个参数,而不是四个参数:chunk
,bytes_remaining
和{{1}}。
答案 4 :(得分:1)
您也可以这样做,而无需编写自己的函数。
代码:
from pytube import YouTube
from pytube.cli import on_progress #this module contains the built in progress bar.
link=input('enter url:')
yt=YouTube(link,on_progress_callback=on_progress)
videos=yt.streams.first()
videos.download()
print("(:")
答案 5 :(得分:0)
from pytube import Playlist
from pytube import YouTube
previousprogress = 0
def on_progress(stream, chunk, bytes_remaining):
global previousprogress
total_size = stream.filesize
bytes_downloaded = total_size - bytes_remaining
liveprogress = (int)(bytes_downloaded / total_size * 100)
if liveprogress > previousprogress:
previousprogress = liveprogress
print(liveprogress)
yt = YouTube('https://www.youtube.com/watch?v=4zqKJBxRyuo&ab_channel=SleepEasyRelax-KeithSmith')
yt.register_on_progress_callback(on_progress)
yt.streams.filter(only_audio=True).first().download()
答案 6 :(得分:0)
您可以像这样添加进度条。忽略愚蠢的类型错误(如果有)
pytube.request.default_range_size = 1048576 # this is for chunck size, 1MB size
yt = YouTube(url)
video = yt.streams.first()
video.download(<whatever>)
def progress_callback(stream, chunk, bytes_remaining):
size = video.filesize
progress = int(((size - bytes_remaining) / size) * 100)
print(progress)
# do call progress bar from GUI here
def complete_callback(stream, file_handle):
print("downloading finished")
# progress bar stop call from GUI here
yt.register_on_progress_callback(progress_callback)
yt.register_on_complete_callback(complete_callback)
答案 7 :(得分:0)
这很有趣!
我们可以用如下代码模拟linux的下载动画:
// eslint-disable-next-line
输出看起来像:
↳ |██████████████████████████████████----------- -----| 68.4%
玩得开心!!