我的 python youtube 视频下载器有问题

problem with my python youtube video downloader

大家好,我是 python 的新手,我正在尝试制作 YouTube 视频下载器,代码可以正常工作,但是当视频下载完成时没有显示完成消息,我不知道为什么

代码:

from pytube import YouTube
link = input("VIDEO URL: ")

video = YouTube(link)

def done():
    print("Download Complete")

High_Quality_720p = video.streams.get_highest_resolution().download(output_path="/Users/tahaw/OneDrive/Desktop/download_path")
def highQ_vid_comp():
    print(video.register_on_complete_callback("Download Complete 720p"))

Low_Quality_360p = video.streams.get_lowest_resolution().download(output_path="/Users/tahaw/OneDrive/Desktop/download_path")
def lowQ_vid_comp():
    print(video.register_on_complete_callback("Download Complete 360p"))



for video in video.streams.filter(progressive=True):
    print(video)

res=input("Resolution(720p/360p): ")

if res == ("720p"):
    print(High_Quality_720p)
    highQ_vid_comp()
elif res == ("360p"):
    print(Low_Quality_360p)
    lowQ_vid_comp()

你的代码一团糟。

您在 select 分辨率之前运行了 download() 两次 - 所以您下载了两个分辨率。

register_on_complete_callback 不需要字符串,而是函数的名称,它将在完成时执行。

from pytube import YouTube

# --- functions ---

def on_progress(stream, chunk, bytes_remaining):
    print(' progress:', bytes_remaining, "           ", end='\r', flush=True)

def on_complete(stream, filename):
    print()
    print('--- Completed ---')
    print('stream:', stream)
    print('filename:', filename)
    
# --- main ---

# variables and CONSTANTS
OUTPUT_PATH = "/Users/tahaw/OneDrive/Desktop/download_path"
#OUTPUT_PATH = ""

# questions
link = input("VIDEO URL: ")
if not link:
    link = 'https://www.youtube.com/watch?v=aqz-KE-bpKQ'

video = YouTube(link)
video.register_on_progress_callback(on_progress)
video.register_on_complete_callback(on_complete)

#for item in video.streams.filter(progressive=True):
#    print('resolution:', item)

res = input("Resolution(720p/360p): ")

# getting stream

if res == "720p":
    selected_stream = video.streams.get_highest_resolution()
elif res == "360p":
    selected_stream = video.streams.get_lowest_resolution()
else:
    selected_stream = None
    
# downloading

if selected_stream:    
    filename = selected_stream.download(output_path=OUTPUT_PATH)
else:
    filename = '???'
    
print('--- After ---')    
print('filename:', filename)