Fetch youtube video resolution in python pytube

Question:

I am trying to make a youtube video downloader using pytube.
So, I need different video resolution which is available for this video(example: 360p, 480p, 720p…)

my code is given bellow:

from pytube import YouTube
yt = YouTube("https://youtu.be/xyzxyzxyz")
print(yt.streams[0])

output:

<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2" progressive="True" type="video">

data type of this output is:

<class 'pytube.streams.Stream'>

now I want to fetch this (res="360p") separately.

how can I do it?

Asked By: Al Sayeed

||

Answers:

You might want to try this:

from pytube import YouTube
yt = YouTube("https://www.youtube.com/watch?t=27&v=RjLH2vE5rpk")
print(yt.streams.get_highest_resolution().resolution)

Output:

360p
Answered By: baduker

You can use filter method defined on api. Hence:

streams = yt.streams.filter(res="360p")

Example:

from pytube import YouTube
url = 'https://www.youtube.com/watch?v=NVPZM_jy-sk&ab_channel=MOKS346'
yt = YouTube(url)
streams = yt.streams.filter(res="360p")
for s in streams:
    print(s)

Output:

<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2" progressive="True" type="video">
<Stream: itag="134" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.4d401e" progressive="False" type="video">
<Stream: itag="243" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp9" progressive="False" type="video">
<Stream: itag="396" mime_type="video/mp4" res="360p" fps="30fps" vcodec="av01.0.01M.08" progressive="False" type="video">
Answered By: Gabriel Bento
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.