PyTube printing video resolutions

Question:

Is there a way to print all the video resolutions clearly

from pytube import YouTube

# enter video URL
video_url = ""

# create youtube thing
yt = YouTube(video_url)

# Get list of all resolutions
resolutions = []
for stream in yt.streams.filter(progressive=True):
    resolutions.append(stream.resolution)

# sort resolutions and print
resolutions.sort()
print("Available Resolutions: ")
for res in resolutions:
    print(res)

Output for this code:

Available Resolutions: 
144p
360p
720p

but there is more resolutions why cant i see and i want to print all of them clear like this

Asked By: Hasan Hüseyin

||

Answers:

To print all available video resolutions for a YouTube video using PyTube, you can do the following:

  1. Include both progressive and adaptive streams.
  2. Use a set to store unique resolutions and filter out audio-only streams.
  3. Sort and print the resolutions.

Here’s the code:

from pytube import YouTube

video_url = "https://www.youtube.com/watch?v=RbmS3tQJ7Os"  # Enter the video URL

yt = YouTube(video_url)
resolutions = sorted(
    {stream.resolution for stream in yt.streams if stream.resolution},
    key=lambda x: int(x[:-1]),
)

print("Available Resolutions:")
for res in resolutions:
    print(res)

You can change my video ("https://www.youtube.com/watch?v=RbmS3tQJ7Os") with any video you like.

Answered By: cconsta1

The reason why you are not getting all the available resolutions is that you are only considering the progressive streams in your code.

Progressive streams typically are available in a limited range of resolutions, such as 240p, 360p, 480p, etc. and on the other hand, Adaptive streams are available in a wider range of resolutions, including higher resolutions such as 1080 1440 and (4K).

  from pytube import YouTube

# enter video URL
video_url = ""

# create youtube thing
yt = YouTube(video_url)

# Get list of all resolutions
resolutions = []
for stream in yt.streams:
    if stream.includes_video_track:
        resolutions.append(stream.resolution)

# sort resolutions and print
resolutions = sorted(set(resolutions))
print("Available Resolutions: ")
for res in resolutions:
    print(res)

This should give you a list of all available resolutions for the video.

Answered By: PandaSurge
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.