Skip to content

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.

example.py
1
2
3
4
5
from youtube_dl_scraper import YouTube
youtube = YouTube()
video = youtube.scrape_video('https://youtu.be/sF9xYtouZjY?si=z6ZWk4raQeHgQDz')

print(video.title) # print video title

We'll beging by running the following to list all streams.

# ...
print(video.streams) # (1)!
  1. 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.

print(video.streams[0:3])
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))

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.

print(video.streams.get_video_streams())
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),
(video 144p 30fps is_hdr=False has_audio=True))
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.

print(video.streams.get_video_streams().filter(has_audio=True)) # with audio
print(video.streams.get_video_streams().filter(has_audio=False)) # without audio
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),
(video 144p 30fps is_hdr=False has_audio=True))
()

Filtering Audio Streams

To filter video streams we use the get_audio_streams() method.

print(video.streams.get_audio_streams())
output
((audio 320), (audio 192), (audio 128),
(audio 64), (audio 32))

Ordering Streams

YouTube DL Scraper support the ability to order streams by any of it's attributes using the order_by() method.

print(video.streams.get_audio_streams().order_by("abr")) # (1)!
  1. this returns a new StreamArray object with streams ordered by abr
output
((audio 320), (audio 192), (audio 128), (audio 64), (audio 32))

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.

print(video.streams.filter(resolution_value=720).download())
Tip

You can use first() and last() method to select the first or last Stream in the StreamArray.

print(video.streams.first())
print(video.streams.get_video_streams().last())
And you can get the available qualities in a stream array using the available_qualities property.
print(video.streams.available_qualities) # (1)!

  1. Output: {'video': (1080, 720, 360, 360, 144), 'audio': (320, 192, 128, 64, 32)}

Lastly you can fetch streams using their index.

print(video.streams[0]) # getting first stream

You can read more from the api docs for youtube_dl_scraper.core.stream_array.StreamArray