How to add progress bar?

ⅰ亾dé卋堺 提交于 2019-12-02 04:41:17
Ismael GraHms

Call your progress function inside the Youtube class

yt = YouTube(video_link, on_progress_callback=progress_func)

This is your progress function

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)

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

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:

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)

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

Somewhat shorter option:

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...')

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!