Skip to content

Commit 7a4446f

Browse files
Update you_tube_analyzer.py
1 parent cb3a1b7 commit 7a4446f

1 file changed

Lines changed: 34 additions & 100 deletions

File tree

Lines changed: 34 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,34 @@
1-
# Import necessary classes from the pytube library
2-
from pytube import YouTube, exceptions, Playlist, Channel, Search
3-
4-
# Prompt the user to select an option: Video, Playlist, Channel information, or Search
5-
dat = int(
6-
input(
7-
"""Select The option:
8-
1. Video 2. Playlist 3. Channel information 4. Search :\n"""
9-
)
10-
)
11-
12-
13-
# Function to handle downloading a single video
14-
def Video():
15-
# Prompt the user to enter the URL of the video
16-
url = input("Enter the video URL: ")
17-
18-
try:
19-
# Try to create a YouTube object for the provided URL
20-
yt = YouTube(url)
21-
except exceptions.VideoUnavailable:
22-
# If the video is unavailable or has been removed, catch the VideoUnavailable exception
23-
print("Video Unavailable")
24-
except exceptions.VideoPrivate:
25-
# If the video is private, catch the VideoPrivate exception
26-
print("Video is Private")
27-
except exceptions.AgeRestrictedError:
28-
# If the video is age-restricted, catch the AgeRestrictedError exception
29-
print("Video is age-restricted")
30-
else:
31-
# If no exception occurred, display available streams for the video
32-
streams = yt.streams.all()
33-
vid = list(enumerate(streams))
34-
for i in vid:
35-
print(i)
36-
# Prompt the user to select a stream by its index
37-
strm = int(input("Enter the index of the stream: "))
38-
# Display video information and download the selected stream
39-
print("Title: ", yt.title)
40-
print("Number of views: ", yt.views)
41-
print("Length of video: ", yt.length)
42-
print("Rating of video: ", yt.rating)
43-
streams[strm].download()
44-
# Notify the user that the video download process is complete
45-
print("Video Downloaded successfully...")
46-
47-
48-
# Function to download a playlist
49-
def download_playlist(playlist_url):
50-
# Create a Playlist object for the provided playlist URL
51-
playlist = Playlist(playlist_url)
52-
# Download each video in the playlist with the highest resolution available
53-
for video in playlist.videos:
54-
video.streams.get_highest_resolution().download()
55-
# Notify the user that the playlist download process is complete
56-
print("Playlist downloaded successfully..")
57-
58-
59-
# Function to get information about a YouTube channel
60-
def channel():
61-
# Prompt the user to enter the URL of the channel
62-
channel_link = input("Enter Channel Link: ")
63-
# Create a Channel object for the provided channel URL
64-
channel = Channel(channel_link)
65-
# Display basic information about the channel
66-
print("Channel ID: " + channel.channel_id)
67-
print("Channel Name: " + channel.channel_name)
68-
# Get the number of videos in the channel and display it
69-
print("No. of videos in the channel: " + str(len(channel.video_urls)))
70-
71-
72-
# Function to perform a YouTube search and display results
73-
def search():
74-
# Prompt the user to enter the search query
75-
search_query = input("Enter Search Query: ")
76-
# Create a Search object for the provided search query
77-
s = Search(search_query)
78-
# Display search results (video URLs)
79-
for i in s.results:
80-
print(i)
81-
print("\n")
82-
# Display search suggestions related to the topic
83-
print("Suggestions regarding the topic:\n")
84-
for j in s.completion_suggestions:
85-
print(j)
86-
87-
88-
# Based on the user's input, execute the appropriate function
89-
if dat == 1:
90-
Video()
91-
elif dat == 2:
92-
# If the user selected option 2, prompt for a playlist URL and download it
93-
playlist_url = input("Enter the playlist URL: ")
94-
download_playlist(playlist_url)
95-
elif dat == 3:
96-
# If the user selected option 3, get information about a YouTube channel
97-
channel()
98-
elif dat == 4:
99-
# If the user selected option 4, perform a YouTube search and display results
100-
search()
1+
import yt_dlp
2+
3+
def download_youtube_video(video_url):
4+
"""Downloads a YouTube video using yt-dlp."""
5+
6+
ydl_opts = {
7+
'outtmpl': '%(title)s.%(ext)s', # Output filename template
8+
}
9+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
10+
info_dict = ydl.extract_info(video_url, download=False)
11+
formats = info_dict.get('formats', None)
12+
13+
# Print all the available formats and ask the user to select
14+
for f in formats:
15+
print(f"{f['format_id']}:\t{f['ext']} ({f.get('format_note', None)}p)")
16+
17+
resolution_choice = input("Do you want to select from the available options? (y/n): ")
18+
19+
if resolution_choice.lower() == 'y':
20+
format_id = input("Enter the format id of the video: ")
21+
ydl_opts['format'] = format_id
22+
else:
23+
# Select the highest resolution format
24+
highest_resolution = max(formats, key=lambda x: x.get('height', 0))
25+
format_id = highest_resolution['format_id']
26+
ydl_opts['format'] = format_id
27+
28+
# Download the video
29+
ydl.download([video_url])
30+
print("Download complete using yt-dlp!")
31+
32+
if __name__ == "__main__":
33+
video_url = input("Enter the URL: ")
34+
download_youtube_video(video_url)

0 commit comments

Comments
 (0)