You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Telethon下载视频时TQDM进度条不增长问题求助

问题排查与解决方案

核心问题是tqdm初始化的total单位和进度回调中update的数值单位不匹配,导致进度条计算的百分比趋近于0,具体分析和修复如下:

问题原因

你的代码中:

  1. tqdm初始化时,total=file_size(推测file_size是字节数,Telethon返回的total通常以字节为单位)
  2. 进度回调里,你把current转换为MB((current/1024)/1024),然后用这个MB数值去更新bar,但bar的total是字节数。

比如终端显示的1.38/2.21M,实际bar的total是2.21*1024*1024≈2317000字节,而你每次更新的是1.38左右的数值,相对于2317000来说占比极小,所以进度条百分比始终显示0%。

修复方案

方案1:统一使用字节单位(推荐,无需额外转换)

保持tqdm的total为字节数,直接用current字节值更新进度条:

修改后的tqdm初始化代码:

global bar 
# 直接用字节作为total,开启unit_scale自动转换显示单位
bar = tqdm(total=file_size, unit="B", unit_scale=True, desc="Download progress")

修改后的进度回调函数:

async def progress(current, total): 
    global bar 
    # 计算当前已下载字节与bar已记录值的差值,直接更新
    update_var = current - bar.n 
    bar.update(update_var) 
    # 仅在编辑消息时转换为MB显示
    downloaded_mb = current / (1024 * 1024)
    full_size_mb = total / (1024 * 1024)
    progresss = f'{downloaded_mb:.2f}/{full_size_mb:.2f} MB' 
    await client.edit_message(msg, f'Downloading video... {progresss}')

方案2:统一使用MB单位

如果希望tqdm内部以MB为单位计算,初始化时将total转换为MB:

修改后的tqdm初始化代码:

global bar 
# 将字节数转换为MB作为total,指定单位为MB
bar = tqdm(total=file_size/(1024*1024), unit="MB", desc="Download progress")

修改后的进度回调函数:

async def progress(current, total): 
    global bar 
    # 将当前下载字节转换为MB
    downloaded_mb = current / (1024 * 1024)
    # 计算MB差值并更新
    update_var = downloaded_mb - bar.n 
    bar.update(update_var) 
    full_size_mb = total / (1024 * 1024)
    progresss = f'{downloaded_mb:.2f}/{full_size_mb:.2f} MB' 
    await client.edit_message(msg, f'Downloading video... {progresss}')

额外注意事项

  • 避免使用global变量传递tqdm实例,推荐用类封装或者闭包的方式,减少全局变量依赖
  • Telethon的progress_callback会频繁调用(默认每1秒一次),确保edit_message的频率不会触发平台限制

内容的提问来源于stack exchange,提问作者Stan Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 08:29:53