如何添加进度条? [英] How to add progress bar?

查看:38
本文介绍了如何添加进度条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 pytube 中添加进度条?我不知道如何使用以下方法:

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)

我的代码:

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

推荐答案

在 Youtube 类中调用进度函数

Call your progress function inside the Youtube class

yt = YouTube(video_link, on_progress_callback=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

这篇关于如何添加进度条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆