Skip to content

Reference for youtube_dl_scraper/core/video.py

youtube_dl_scraper.core.video.Video

Video(video_data: dict, download_path: str)

Data class representing a video with streams and captions.

Parameters:

Name Type Description Default
video_data dict

A dictionary containing video metadata such as ID, title, etc.

required
download_path str

The directory path where the video and its streams will be saved.

required
Source code in youtube_dl_scraper/core/video.py
def __init__(self, video_data: dict, download_path: str):
    """
    Initialize the Video object with data and set up basic properties.

    Args:
        video_data (dict): A dictionary containing video metadata such as ID, title, etc.
        download_path (str): The directory path where the video and its streams will be saved.
    """
    self.raw_video_data = video_data
    self.download_path = download_path
    self.id = video_data.get("id")
    self.title = video_data.get("title")
    self.title_slug = video_data.get("title_slug") or title_to_slug(self.title)
    self.watch_url = video_data.get("watch_url")
    self.duration = video_data.get("duration")
    self.formatted_duration = video_data.get(
        "formatted_duration"
    ) or format_duration(self.duration)
    self.fduration = self.formatted_duration  # short hand to formatted_duration
    self.thumbnail = video_data.get("thumbnail")
    self._get_captions = None
captions property
captions: CaptionArray

Property that retrieves the captions for the video.

Returns:

Name Type Description
CaptionArray CaptionArray

The captions for the video.

Raises:

Type Description
NotImplementedError

If captions are not available.

streams property
streams: StreamArray

Property that retrieves the streams for the video.

Returns:

Name Type Description
StreamArray StreamArray

The parsed video and audio streams.

Raises:

Type Description
KeyError

If the "streams" key is missing in the raw video data.

parse_streams
parse_streams(streams: dict) -> StreamArray

Parse video and audio streams from the given stream data.

Parameters:

Name Type Description Default
streams dict

A dictionary containing stream data with keys 'video' and 'audio'.

required

Returns:

Name Type Description
StreamArray StreamArray

An object containing parsed video and audio streams.

Source code in youtube_dl_scraper/core/video.py
def parse_streams(self, streams: dict) -> StreamArray:
    """
    Parse video and audio streams from the given stream data.

    Args:
        streams (dict): A dictionary containing stream data with keys 'video' and 'audio'.

    Returns:
        StreamArray: An object containing parsed video and audio streams.
    """
    video_streams = streams.get("video", [])
    video_api = streams.get("video_api", {})
    audio_streams = streams.get("audio", [])
    audio_api = streams.get("audio_api", {})

    streams = StreamArray(video_api=video_api, audio_api=audio_api)

    # Adding video streams
    for stream in video_streams:
        vid = VideoStream(
            stream, file_name=self.title_slug, download_path=self.download_path
        )
        streams.add_stream(vid)

    # Adding audio streams
    for stream in audio_streams:
        aud = AudioStream(
            stream, file_name=self.title_slug, download_path=self.download_path
        )
        streams.add_stream(aud)

    return streams