Search code examples
pythonpython-3.xpytube

How to extract elements from pytube stream?


I am working with pytube to write an automated scripts which helps me download a YouTube video. I wanted to get the itag number of the highest resolution video.

The method get_highest_resolution() returns the stream of the highest resolution video:

highest_resolution_stream = yt_obj.streams.filter(progressive=True, file_extension='mp4').get_highest_resolution()

Its output looks like this:

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

Now I want to extract the value of itag from this pytube stream.


Solution

  • To extract itag, we just need to do use <stream>.itag:

    highest_resolution_stream = yt_obj.streams.filter(progressive=True, file_extension='mp4').get_highest_resolution()
    my_itag = highest_resolution_stream.itag