hi guys I’m new to python and i was trying to make a YouTube video downloader, the code is working but the completion message is not shown when video download is completed and I don’t know why
the code:
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()
Advertisement
Answer
You have big mess in code.
You runs download()
two times before you even select resolution – so you download both resolutions.
register_on_complete_callback
doesn’t expect string but function’s name which it will execute on complete.
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)