首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在输油管道中使用进度条?

如何在输油管道中使用进度条?
EN

Stack Overflow用户
提问于 2019-05-18 09:56:59
回答 4查看 4.5K关注 0票数 1

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

How to add progress bar?这个补丁在最新版本中不起作用。

这是最新的文档https://pypi.org/project/pytube/

代码语言:javascript
复制
from pytube import YouTube
url="https://youtu.be/J5EXnh53A1k"
path=r'D://'
yt = YouTube(url)
yt.register_on_progress_callback(show_progress_bar)#by commenting this line code works fine but no progress bar is displyed
yt.streams.filter(file_extension='mp4').first().download(path)


def show_progress_bar(stream, _chunk, _file_handle, bytes_remaining):
  current = ((stream.filesize - bytes_remaining)/stream.filesize)
  percent = ('{0:.1f}').format(current*100)
  progress = int(50*current)
  status = '█' * progress + '-' * (50 - progress)
  sys.stdout.write(' ↳ |{bar}| {percent}%\r'.format(bar=status, percent=percent))
  sys.stdout.flush()
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-06-14 05:17:25

我用的是progressbar2

代码语言:javascript
复制
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()
票数 2
EN

Stack Overflow用户

发布于 2021-07-03 07:35:28

您首先需要定义进度条函数,比如progress_function

代码语言:javascript
复制
def progress_function(chunk, file_handle, bytes_remaining):
    global filesize
    current = ((filesize - bytes_remaining)/filesize)
    percent = ('{0:.1f}').format(current*100)
    progress = int(50*current)
    status = '█' * progress + '-' * (50 - progress)
    sys.stdout.write(' ↳ |{bar}| {percent}%\r'.format(bar=status, percent=percent))
    sys.stdout.flush()

然后将上面定义的函数progress_function注册到on_progress_callback中,如下所示:

代码语言:javascript
复制
yt_obj = YouTube(<<youtube_video_url>>, on_progress_callback = progress_function)

其余代码如下:

代码语言:javascript
复制
yt_obj.streams.filter(progressive=True, file_extension='mp4').get_highest_resolution().download(output_path='/home/myusername/Videos', filename='MyVideo')

输出如下:

|██████████████████████████████████----------------|↳68.4%

玩得开心!!

票数 2
EN

Stack Overflow用户

发布于 2021-04-27 19:17:17

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

代码语言:javascript
复制
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)

显示颜色被切换,因为默认进度条相当明亮。在以前下载视频的情况下,将显示“完成下载:”消息,但不会显示进度条。

请看这个Showing progress in pytube关于使用管道的内置on_progress函数.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56197879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档