-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
52 lines (38 loc) · 1.6 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import pytube
# enter the YouTube playlist URL
PLAYLIST_URL = input("Enter the YouTube playlist URL: ")
# enter the start and end video numbers
START_VIDEO = int(input("Enter the start video number: "))
END_VIDEO = int(input("Enter the end video number: "))
# enter the output directory
output_dir = input("Enter the output directory: ")
# Create a PyTube playlist object
playlist = pytube.Playlist(PLAYLIST_URL)
# Get a list of all the video URLs in the playlist
video_urls = playlist.video_urls
# Iterate over the list of video URLs
for index, url in enumerate(video_urls[START_VIDEO - 1:END_VIDEO]):
# Create a PyTube video object
video = pytube.YouTube(url)
try:
# Get the video's title
video_title = video.title
except pytube.exceptions.PytubeError:
# If error occurs, fallback title
video_title = "Unknown_Title"
# Calculate the current video number
current_video_number = START_VIDEO + index
# Print the video's title with the current video number
print(f"Downloading {current_video_number}: '{video_title}' ...")
# Check if the output directory exists, and create it if it doesn't
if not os.path.exists(output_dir):
os.mkdir(output_dir)
# Try to get a 720p video stream
video_stream = video.streams.get_highest_resolution()
# If no 720p stream is available, try to get the highest resolution stream
if video_stream is None:
video_stream = video.streams.get_highest_resolution()
# Download the video to the output directory
video_stream.download(output_dir)
print("Finished downloading videos!")