0

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()
3
  • you definde print("Download Complete") inside done() but you never execute done() so it doesn't display it. Commented Apr 14, 2021 at 7:16
  • are you sure it should be text in register_on_complete_callback(..) ? Maybe it should be function's name like register_on_complete_callback(done) to execute done() when it is completed. Commented Apr 14, 2021 at 7:18
  • is this code really works ? It gives me error 'Stream' object has no attribute 'register_on_complete_callback'. As for me it seems one mess. Commented Apr 14, 2021 at 7:23

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

tysm you helped me a lot ❤

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.