Working With Streams and StreamArrays¶
The Stream and StreamArray classes are core components of the youtube_dl_scraper
package, enabling easy access and manipulation of video and audio streams. These classes provide a clean interface to work with individual streams or groups of streams.
Stream¶
The Stream class represents a single video or audio stream. It provides detailed information about the stream and includes methods for further interaction.
StreamArray¶
The StreamArray class represents a collection of Stream objects. It includes utility methods for filtering and selecting streams based on specific criteria.
Key Features¶
- Retrieve all streams matching a specific quality or format.
- Iterate over all available streams.
- And much more.
These classes streamline the process of managing YouTube video/audio streams, making it easy to access and utilize downloadable media. For more advanced use cases, explore the methods and attributes available within the Stream and StreamArray classes.
Working with Streams¶
We'll begin with the video object from the previous example stored in the video
variable.
Read more
From the previous example.
We'll beging by running the following to list all streams.
- this returns the StreamArray object containing the available streams in the video.
output
((video 1080p 30fps is_hdr=False has_audio=True),
(video 720p 30fps is_hdr=False has_audio=True),
(video 360p 30fps is_hdr=False has_audio=True),
(video 360p 30fps is_hdr=False has_audio=True),
(audio 320), (audio 192),
(video 144p 30fps is_hdr=False has_audio=True),
(audio 128), (audio 64), (audio 32))
Accessing Streams¶
YouTube DL Scraper allows the accessing of specific streams based on index. It also supports slicing of StreamArrays.
output
Filtering Streams¶
YouTube DL Scraper provides built-in functionality to filter streams available in a StreamArray object using the filter()
method. This method supports various keyword arguments, allowing you to customize your search. Below, we’ll review some of the most commonly used options. For a complete list of filterable properties, refer to the API documentation for youtube_dl_scraper.core.stream_array.StreamArray.filter.
Filtering Video Streams¶
To filter video streams we use the get_video_streams()
method.
output
more on video streams
Not all streams has audio, streams without audio are called DASH streams while ones with audio are Progressive streams. To filter video streams to ones with or without audio we use the filter()
method.
Filtering Audio Streams¶
To filter video streams we use the get_audio_streams()
method.
Ordering Streams¶
YouTube DL Scraper support the ability to order streams by any of it's attributes using the order_by()
method.
- this returns a new StreamArray object with streams ordered by abr
Downloading Streams¶
After you’ve selected the Stream you’re interested, you’re ready to interact with it. At this point, you can query information about the stream, such as its size, whether the stream is audio/video, and more. You can also use the download method to save the file.
Tip
You can use first()
and last()
method to select the first or last Stream in the StreamArray.
available_qualities
property.
- Output:
{'video': (1080, 720, 360, 360, 144), 'audio': (320, 192, 128, 64, 32)}
Lastly you can fetch streams using their index.
You can read more from the api docs for youtube_dl_scraper.core.stream_array.StreamArray