Skip to content Skip to sidebar Skip to footer

How To Add Progress Bar?

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

Solution 1:

Call your progress function inside the Youtube class

yt = YouTube(video_link, on_progress_callback=progress_function)

This is your progress function

defprogress_function(self,stream, chunk,file_handle, bytes_remaining):

    size = stream.filesize
    p = 0while p <= 100:
        progress = p
        printstr(p)+'%'
        p = percent(bytes_remaining, size)

This computes the percentage converting the file size and the bytes remaining

defpercent(self, tem, total):
        perc = (float(tem) / float(total)) * float(100)
        return perc

Solution 2:

The callback function takes three arguments, not four: stream, chunk and bytes_remaining.

Solution 3:

I know this is already answered, but I came across this and for me, the progress was counting down from 100 to 0. Since I wanted to fill a progress bar with the percentage value, I couldn't use this.

So I came up with this solution:

defprogress_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)

The loadbar is my Progress Bar from PyQt5. Hope this helps someone.

Solution 4:

You can also do like this without writing your own function.

code:

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("(:")

Solution 5:

Somewhat shorter option:

yt = YouTube(video_link, on_progress_callback=progress_function)

video = yt.streams.first() # or whatever # Prints something like "15.555% done..." defprogress_function(stream, chunk, file_handle, bytes_remaining):
    print(round((1-bytes_remaining/video.filesize)*100, 3), '% done...')

You can, of course, limit the progress output, for instance, to values like 10, 20, 30%... - just surround the print statement with the required if-clause.

Post a Comment for "How To Add Progress Bar?"