Python 3.x 如何在pytube中使用进度条?

Python 3.x 如何在pytube中使用进度条?,python-3.x,pytube,Python 3.x,Pytube,我想在我的代码中实现一个进度条,但是新旧的实现方式都不起作用 此修复程序在最新版本中不起作用。 这是最新的文档 我正在使用progressbar2 def progress_Check(stream = None, chunk = None, file_handle = None, remaining = None): percent = file_size - remaining + 1000000 try: # upda

我想在我的代码中实现一个进度条,但是新旧的实现方式都不起作用

此修复程序在最新版本中不起作用。
这是最新的文档


我正在使用progressbar2

def progress_Check(stream = None, chunk = None, file_handle = None, remaining = None):

        percent = file_size - remaining + 1000000   

        try: 
            # updates the progress bar                                   
            bar.update(round(percent/1000000,2))
        except: 
            # progress bar dont reach 100% so a little trick to make it 100    
            bar.update(round(file_size/1000000,2))

yt = YouTube(url, on_progress_callback=progress_Check)
yt = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download()

以下是用于从shell下载youtube视频和显示进度条的功能:

from pytube import YouTube
from pytube.cli import on_progress

fuchsia = '\033[38;2;255;00;255m'   #  color as hex #FF00FF
reset_color = '\033[39m'

# url is url of youtube video to download.
def download_youtube(url):

    """ Instantiates YouTube class and downloads selected video.  Uses Built-in
    pytube.cli function on_progress to show a DOS style progress bar. """
    yt = YouTube(url, on_progress_callback=on_progress)

    # following line displays title and number of times video has been viewed. 
    print(f'\n' + fuchsia + 'Downloading: ', yt.title, '~ viewed', yt.views, 
    'times.')

    # creates download and downloads to subdirectory called 'downloads'
    yt.streams.first().download('.\\downloads\\')

    # displays message verifying download is complete, and resets color scheme 
    # back to original color scheme.
    print(f'\nFinished downloading:  {yt.title}' + reset_color)
显示颜色已切换,因为默认进度条相当亮。如果之前下载了视频,则会显示“已完成下载:”消息,但不会显示进度条


关于pytube内置的on_progress功能的使用,请参见此部分。

为什么要添加1000000?
# importing YouTube from pytube

import progressbar as progress
from pytube import YouTube


def progress(streams, chunk: bytes, bytes_remaining: int):
    contentsize = video.filesize
    size = contentsize - bytes_remaining

    print('\r' + '[Download progress]:[%s%s]%.2f%%;' % (
    '█' * int(size*20/contentsize), ' '*(20-int(size*20/contentsize)), float(size/contentsize*100)), end='')


url = 'https://www.youtube.com/watch?v=qOVAbKKSH10'
yt = YouTube(url, on_progress_callback=progress)
video = yt.streams.get_highest_resolution()
video.download()
from pytube import YouTube
from pytube.cli import on_progress

fuchsia = '\033[38;2;255;00;255m'   #  color as hex #FF00FF
reset_color = '\033[39m'

# url is url of youtube video to download.
def download_youtube(url):

    """ Instantiates YouTube class and downloads selected video.  Uses Built-in
    pytube.cli function on_progress to show a DOS style progress bar. """
    yt = YouTube(url, on_progress_callback=on_progress)

    # following line displays title and number of times video has been viewed. 
    print(f'\n' + fuchsia + 'Downloading: ', yt.title, '~ viewed', yt.views, 
    'times.')

    # creates download and downloads to subdirectory called 'downloads'
    yt.streams.first().download('.\\downloads\\')

    # displays message verifying download is complete, and resets color scheme 
    # back to original color scheme.
    print(f'\nFinished downloading:  {yt.title}' + reset_color)