|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +if TYPE_CHECKING: |
| 6 | + from daft.dataframe.dataframe import DataFrame |
| 7 | + from daft.daft import IOConfig |
| 8 | + |
| 9 | +__all__ = [ |
| 10 | + # TODO: additional video support |
| 11 | + # "read_audio_frames", |
| 12 | + # "read_audio_streams", |
| 13 | + # "read_audio_streams_metadata", |
| 14 | + # "read_subtitle_frames", |
| 15 | + # "read_subtitle_streams", |
| 16 | + # "read_subtitle_streams_metadata", |
| 17 | + "read_video_frames", |
| 18 | + # "read_video_streams", |
| 19 | + # "read_video_streams_metadata", |
| 20 | +] |
| 21 | + |
| 22 | + |
| 23 | +def read_video_frames( |
| 24 | + path: str | list[str], |
| 25 | + image_height: int, |
| 26 | + image_width: int, |
| 27 | + is_key_frame: bool | None = None, |
| 28 | + io_config: IOConfig | None = None, |
| 29 | +) -> DataFrame: |
| 30 | + """Creates a DataFrame by reading the frames of one or more video files. |
| 31 | +
|
| 32 | + This produces a DataFrame with the following fields: |
| 33 | + * path (string): path to the video file that produced this frame. |
| 34 | + * frame_index (int): frame index in the video. |
| 35 | + * frame_time (float): frame time in fractional seconds as a floating point. |
| 36 | + * frame_time_base (str): fractional unit of seconds in which timestamps are expressed. |
| 37 | + * frame_pts (int): frame presentation timestamp in time_base units. |
| 38 | + * frame_dts (int): frame decoding timestamp in time_base units. |
| 39 | + * frame_duration (int): frame duration in time_base units. |
| 40 | + * is_key_frame (bool): true iff this is a key frame. |
| 41 | +
|
| 42 | + Warning: |
| 43 | + This requires PyAV which can be installed with `pip install av`. |
| 44 | +
|
| 45 | + Note: |
| 46 | + This function will stream the frames from all videos as a DataFrame of images. |
| 47 | + If you wish to load an entire video into a single row, this can be done with |
| 48 | + read_glob_path and url.download. |
| 49 | +
|
| 50 | + Args: |
| 51 | + path (str|list[str]): Path(s) to the video file(s) which allows wildcards. |
| 52 | + image_height (int): Height to which each frame will be resized. |
| 53 | + image_width (int): Width to which each frame will be resized. |
| 54 | + is_key_frame (bool|None): If True, only include key frames; if False, only non-key frames; if None, include all frames. |
| 55 | + io_config (IOConfig|None): Optional IOConfig. |
| 56 | +
|
| 57 | + Returns: |
| 58 | + DataFrame: dataframe of images. |
| 59 | +
|
| 60 | + Examples: |
| 61 | + >>> df = daft.read_video_frames("/path/to/file.mp4", image_height=480, image_width=640) |
| 62 | + >>> df = daft.read_video_frames("/path/to/directory", image_height=480, image_width=640) |
| 63 | + >>> df = daft.read_video_frames("/path/to/files-*.mp4", image_height=480, image_width=640) |
| 64 | + >>> df = daft.read_video_frames("s3://path/to/files-*.mp4", image_height=480, image_width=640) |
| 65 | + """ |
| 66 | + try: |
| 67 | + from daft.io.av._read_video_frames import _VideoFramesSource |
| 68 | + except ImportError as e: |
| 69 | + raise ImportError("read_video_frames requires PyAV. Please install it with `pip install av`.") from e |
| 70 | + return _VideoFramesSource( |
| 71 | + paths=[path] if isinstance(path, str) else path, |
| 72 | + image_height=image_height, |
| 73 | + image_width=image_width, |
| 74 | + is_key_frame=is_key_frame, |
| 75 | + io_config=io_config, |
| 76 | + ).read() |
0 commit comments