【问题标题】:Downloading YouTube videoes with pafy module with tqdm progressbar使用带有 tqdm 进度条的 pafy 模块下载 YouTube 视频
【发布时间】:2021-05-14 10:52:01
【问题描述】:

我正在尝试使用pafy 模块和使用tqdm 模块的进度条从YouTube 下载视频的代码,但是进度条在下载完成之前完成。

这是我的代码下载片段:

with tqdm.tqdm(desc=video_name, total=video_size, unit_scale=True, unit='B', initial=0) as pbar:
     bestVideo.download(filepath=full_path, quiet=True, callback=lambda _, received, *args: pbar.update(received))

这是进度条的图片:

https://i.stack.imgur.com/VMBUN.png

【问题讨论】:

  • 你是怎么得到video_size的?它的值是否正确 - 您下载的文件是否大小相同?
  • 我得到video_size 正确并且下载完成时下载的文件正在工作,唯一的问题是进度条在下载完成之前完成

标签: python youtube tqdm pafy


【解决方案1】:

问题是因为pbar.update() 期望值current_received - previous_received

download 只给出current_received 所以你必须使用一些变量来记住以前的值并减去它


最少的工作代码:

import pafy
import tqdm

# --- functions ---

previous_received = 0

def update(pbar, current_received):
    global previous_received
    
    diff = current_received - previous_received
    pbar.update(diff)
    previous_received = current_received

# --- main ---

v = pafy.new("cyMHZVT91Dw")
s = v.getbest()

video_size = s.get_filesize()
print("Size is", video_size)
    
with tqdm.tqdm(desc="cyMHZVT91Dw", total=video_size, unit_scale=True, unit='B', initial=0) as pbar:
     s.download(quiet=True, callback=lambda _, received, *args:update(pbar, received))

【讨论】:

  • 谢谢,它工作得很好......我打算改用requests 模块,但这更容易
猜你喜欢
  • 2019-09-23
  • 1970-01-01
  • 2012-08-03
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多