python-3.x - 使用 tqdm 向 youtube dl 添加进度条?
问题描述
嘿,我正在制作一个 python 机器人来使用 youtube_dl 模块下载 youtube 视频,并且无法为其添加进度条任何建议都会有很大帮助
解决方案
我对使用 pytube 的 youtube_dl 模块没有太多经验
代码:
from pytube import YouTube
import os
# on_progress_callback takes 4 parameters.
def progress_Check(stream = None, chunk = None, file_handle = None, remaining = None):
#Gets the percentage of the file that has been downloaded.
percent = (100*(file_size-remaining))/file_size
print("{:00.0f}% downloaded".format(percent))
#Grabs the file path for Download
def file_path():
home = os.path.expanduser('~')
download_path = os.path.join(home, 'Downloads')
return download_path
def start():
print("Your video will be saved to: {}".format(file_path()))
#Input
yt_url = input("Copy and paste your YouTube URL here: ")
print(yt_url)
print ("Accessing YouTube URL...")
# Searches for the video and sets up the callback to run the progress indicator.
try:
video = YouTube(yt_url, on_progress_callback=progress_Check)
except:
print("ERROR. Check your:\n -connection\n -url is a YouTube url\n\nTry again.")
redo = start()
#Get the first video type - usually the best quality.
video_type = video.streams.filter(progressive = True, file_extension = "mp4").first()
#Gets the title of the video
title = video.title
#Prepares the file for download
print ("Fetching: {}...".format(title))
global file_size
file_size = video_type.filesize
#Starts the download process
video_type.download(file_path())
print ("Ready to download another video.\n\n")
again = start()
file_size = 0
begin = start()
推荐阅读
- npm - 电子应用程序不会运行。不知道为什么
- java - IntelliJ - maven 项目,查看/工具窗口没有 maven 选项
- node.js - 如何在 stripe.js 中创建支付意图时传递客户 IP 地址
- javascript - 使用 JavaScript 和锚标记触发文件下载
- azure - Azure 存储容器和来自通配符根域的路由
- python - Python 模块不再导入
- amazon-web-services - AWS ECS 环境属性
- javascript - 在 JSON 文件中存储多个频道 ID
- r - SAP NetWeaver & R
- mysql - SQL - 将一个查询除以另一个