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:
JavaScript
x
30
30
1
from pytube import YouTube
2
link = input("VIDEO URL: ")
3
4
video = YouTube(link)
5
6
def done():
7
print("Download Complete")
8
9
High_Quality_720p = video.streams.get_highest_resolution().download(output_path="/Users/tahaw/OneDrive/Desktop/download_path")
10
def highQ_vid_comp():
11
print(video.register_on_complete_callback("Download Complete 720p"))
12
13
Low_Quality_360p = video.streams.get_lowest_resolution().download(output_path="/Users/tahaw/OneDrive/Desktop/download_path")
14
def lowQ_vid_comp():
15
print(video.register_on_complete_callback("Download Complete 360p"))
16
17
18
19
for video in video.streams.filter(progressive=True):
20
print(video)
21
22
res=input("Resolution(720p/360p): ")
23
24
if res == ("720p"):
25
print(High_Quality_720p)
26
highQ_vid_comp()
27
elif res == ("360p"):
28
print(Low_Quality_360p)
29
lowQ_vid_comp()
30
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.
JavaScript
1
52
52
1
from pytube import YouTube
2
3
# --- functions ---
4
5
def on_progress(stream, chunk, bytes_remaining):
6
print(' progress:', bytes_remaining, " ", end='r', flush=True)
7
8
def on_complete(stream, filename):
9
print()
10
print('--- Completed ---')
11
print('stream:', stream)
12
print('filename:', filename)
13
14
# --- main ---
15
16
# variables and CONSTANTS
17
OUTPUT_PATH = "/Users/tahaw/OneDrive/Desktop/download_path"
18
#OUTPUT_PATH = ""
19
20
# questions
21
link = input("VIDEO URL: ")
22
if not link:
23
link = 'https://www.youtube.com/watch?v=aqz-KE-bpKQ'
24
25
video = YouTube(link)
26
video.register_on_progress_callback(on_progress)
27
video.register_on_complete_callback(on_complete)
28
29
#for item in video.streams.filter(progressive=True):
30
# print('resolution:', item)
31
32
res = input("Resolution(720p/360p): ")
33
34
# getting stream
35
36
if res == "720p":
37
selected_stream = video.streams.get_highest_resolution()
38
elif res == "360p":
39
selected_stream = video.streams.get_lowest_resolution()
40
else:
41
selected_stream = None
42
43
# downloading
44
45
if selected_stream:
46
filename = selected_stream.download(output_path=OUTPUT_PATH)
47
else:
48
filename = '???'
49
50
print('--- After ---')
51
print('filename:', filename)
52