【问题标题】:Pytube how to add a progress bar?Pytube如何添加进度条?
【发布时间】:2020-10-03 06:06:57
【问题描述】:

我不知道该怎么做,每当我尝试其他主题的解决方案时,我都会遇到错误。 主要是“TypeError:show_progress_bar() 缺少 1 个必需的位置参数:'bytes_remaining'”。

from pytube import YouTube

#took this def from another topic
def progress_function(stream, chunk, file_handle, bytes_remaining):
    percent = round((1-bytes_remaining/video.filesize)*100)

    if( percent%10 == 0):
        print(percent, 'done...')



url = "Any youtube url"

yt = YouTube(url, on_progress_callback=progress_function)

yt.streams[0].download()

例如,当我运行这个确切的代码时,它会给我那个错误。

我真的无法理解它的逻辑。我还搜索了 pytube3 网站上的文档,但我无法解决这个问题。请帮助我。谢谢。

【问题讨论】:

  • 也许使用这样的东西:def progress_test(*argv): print(argv) 进行调试。
  • 听起来它向您的函数传递了 3 个参数而不是 4 个,这将证明它向您的自定义进度函数传递了多少参数。
  • 它给出了关于第四个参数“bytes_remaining”的错误
  • 那你能去掉那个论点吗?
  • 实际上是的,当我删除它时,其他参数打印没有错误。感谢这项技术。

标签: python pytube


【解决方案1】:

删除stream就可以了,最近我尝试开发类似的逻辑面临类似的错误。

这是对我有用的代码:

def progress(chunk, file_handle, bytes_remaining):
    global filesize
    remaining = (100 * bytes_remaining) / filesize
    step = 100 - int(remaining)
    print("Completed:", step) # show the percentage of completed download 

一旦您选择要下载的视频或音频,例如

yt = YouTube(str(link), on_progress_callback=progress) # Declare YouTube
yt1 = yt.streams.get_by_itag(int(itag)) # itag is given when you list all the streams of a youtube video
filesize = yt1.filesize

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2019-12-05
    • 1970-01-01
    • 2021-11-14
    • 2019-10-05
    • 2019-09-23
    • 2019-09-16
    • 2012-02-13
    相关资源
    最近更新 更多