首页 > 解决方案 > 使用 tqdm 向 youtube dl 添加进度条?

问题描述

嘿,我正在制作一个 python 机器人来使用 youtube_dl 模块下载 youtube 视频,并且无法为其添加进度条任何建议都会有很大帮助

标签: python-3.x

解决方案


我对使用 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()

推荐阅读