Is there a way to add progress bar in pytube? I don't know how to use the following method:
pytube.Stream().on_progress(chunk, file_handler, bytes_remaining)
My code:
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))
Call your progress function inside the Youtube class
yt = YouTube(video_link, on_progress_callback=progress_function)
This is your progress function
def progress_function(self,stream, chunk,file_handle, bytes_remaining):
size = stream.filesize
p = 0
while p <= 100:
progress = p
print str(p)+'%'
p = percent(bytes_remaining, size)
This computes the percentage converting the file size and the bytes remaining
def percent(self, tem, total):
perc = (float(tem) / float(total)) * float(100)
return perc