Constructor
class Stream(itag, url, filesize)
Represents a media stream from YouTube, which can be either a video, an audio stream, or a combined (muxed) stream.
Parameters:
itag |
str |
Identifier tag for the stream. |
url |
str |
The URL of the stream. |
filesize |
int |
Size of the stream file in bytes. |
Properties
The unique identifier for the stream.
The URL for the stream content.
filesize
int
The size of the stream in bytes.
Flag indicating if the stream is video.
Flag indicating if the stream is audio.
Flag indicating if the stream is muxed (both video and audio).
Video stream width in pixels.
height
int
Video stream height in pixels.
Alias for width of the video stream.
Alias for height of the video stream.
The stream resolution, determined as the smaller value between width and height.
Frame rate (frames per second) of the video.
videoCodec
str
Specifies the codec used for the video stream.
videoExt
str
Indicates the file extension for the video stream (mp4
, webm
).
audioBitrate
int
The bitrate of the audio stream in kilobits per second (kbps).
audioSamplerate
int
The sample rate of the audio stream in Hz.
language
str
Indicates the language of the audio stream, if available.
audioCodec
str
Specifies the codec used for the audio stream.
audioExt
str
Indicates the file extension for the audio stream (mp3
, m4a
).
Methods
get(attribute_name, default=0)
Retrieves the value of any attribute in the Stream object by its name.
If the attribute does not exist, it returns the provided default value.
Parameters:
attribute_name |
str |
The name of the attribute to retrieve. |
default |
Any |
The value to return if the attribute is not found (defaults to 0). |
add_video_info(videoCodec, videoExt, width, height, fps)
Adds specific metadata related to video streams. It also sets the isVideo flag to True, marking this stream as a video stream.
Parameters:
videoCodec |
str |
The codec used for the video stream. |
videoExt |
str |
The file extension of the video stream. |
width |
int |
The width of the video in pixels. |
height |
int |
The height of the video in pixels. |
fps |
int |
The frame rate (frames per second) of the video. |
add_audio_info(audioCodec, audioExt, language, audioBitrate, audioSamplerate)
Adds specific metadata related to audio streams. It also sets the isAudio flag to True, marking this stream as a audio stream.
Parameters:
audioCodec |
str |
The codec used for the audio stream. |
audioExt |
str |
The file extension of the audio stream. |
language |
str |
The language of the audio stream. |
audioBitrate |
int |
The audio bitrate in kilobits per second (kbps). |
audioSamplerate |
int |
The sample rate of the audio stream in Hertz (Hz). |
add_muxed_info(videoCodec, videoExt, width, height, fps, audioCodec, language, audioSamplerate)
Adds metadata for streams that contain both video and audio (muxed streams). It also sets the isMuxed flag to True, marking this stream as a multiplexed stream.
Parameters:
videoCodec |
str |
The codec used for the video portion of the stream. |
videoExt |
str |
The file extension of the muxed stream. |
width |
int |
The width of the video in pixels. |
height |
int |
The height of the video in pixels. |
fps |
int |
The frame rate (frames per second) of the video. |
audioCodec |
str |
The codec used for the audio portion of the stream. |
language |
str |
The language of the audio stream. |
audioSamplerate |
int |
The sample rate of the audio stream in Hertz (Hz). |
async download(*args, **kwargs) ->
str
Downloading the stream asynchronously. Depending on the type of stream (video, audio, or muxed), it initializes an instance of the Downloader class and starts the download.
Returns:
|
str |
The path to the downloaded file. |
Parameters:
Example
pip install prettytable
import MyTube
from prettytable import PrettyTable
yt = MyTube.YouTube("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
t = PrettyTable(['Type', 'Quality', 'ext', 'fps/lang', 'codec'])
for stream in yt.streams:
if stream.isVideo:
t.add_row(['Video',
stream.res,
stream.videoExt,
stream.fps,
stream.videoCodec
])
elif stream.isAudio:
t.add_row(['Audio',
stream.abr,
stream.audioExt,
stream.lang,
stream.audioCodec
])
elif stream.isMuxed:
t.add_row(['Muxed',
stream.res,
stream.videoExt,
f"[{stream.fps}]+[{stream.lang}]",
f"({stream.videoCodec})+({stream.audioCodec})"
])
print(t)
+-------+---------+------+-----------+---------------------------+
| Type | Quality | ext | fps/lang | codec |
+-------+---------+------+-----------+---------------------------+
| Audio | 30 | m4a | en | mp4a.40.5 |
| Audio | 31 | webm | en | opus |
| Audio | 48 | m4a | en | mp4a.40.5 |
| Audio | 46 | webm | en | opus |
| Audio | 61 | webm | en | opus |
| Audio | 129 | m4a | en | mp4a.40.2 |
| Audio | 129 | webm | en | opus |
| Video | 144 | mp4 | 13 | avc1.4d400b |
| Video | 144 | webm | 13 | vp9 |
| Video | 144 | mp4 | 25 | av01.0.00M.08 |
| Video | 144 | mp4 | 25 | avc1.4D400C |
| Video | 144 | webm | 25 | vp09.00.11.08 |
| Video | 240 | mp4 | 25 | av01.0.00M.08 |
| Video | 240 | mp4 | 25 | avc1.4D4015 |
| Video | 240 | webm | 25 | vp09.00.20.08 |
| Video | 360 | mp4 | 25 | av01.0.01M.08 |
| Video | 360 | mp4 | 25 | avc1.4D401E |
| Muxed | 360 | mp4 | [25]+[en] | (avc1.42001E)+(mp4a.40.2) |
| Video | 360 | webm | 25 | vp09.00.21.08 |
| Video | 480 | mp4 | 25 | av01.0.04M.08 |
| Video | 480 | mp4 | 25 | avc1.4D401E |
| Video | 480 | webm | 25 | vp09.00.30.08 |
| Video | 720 | mp4 | 25 | av01.0.05M.08 |
| Video | 720 | mp4 | 25 | avc1.4D401F |
| Video | 720 | webm | 25 | vp09.00.31.08 |
| Video | 1080 | mp4 | 25 | av01.0.08M.08 |
| Video | 1080 | mp4 | 25 | avc1.640028 |
| Video | 1080 | webm | 25 | vp09.00.40.08 |
+-------+---------+------+-----------+---------------------------+