VideoFile
VideoFile extends File and provides additional methods for working with video files.
VideoFile instances are created when a DataChain is initialized from storage with the type="video" parameter:
There are additional models for working with video files:
VideoFrame- represents a single frame of a video file, including its video stream index, frame index, and timestamp.VideoFragment- represents a fragment of a video file.
video_stream_index arguments are zero-based indexes among video streams, matching FFmpeg v:N and PyAV container.streams.video[N] selectors.
VideoFile.get_frame() returns a single frame reference with a timestamp estimated from FPS metadata. Pixel access may seek to a nearby keyframe and decode forward; use VideoFile.get_frames() for sequential access and decoded frame timestamps when available.
These are virtual models that do not create physical files.
Instead, they are used to represent the data in the VideoFile these models are referring to.
If you need to save the data, you can use the save method of these models,
allowing you to save data locally or upload it to a storage service.
VideoFile
Bases: File
A data model for handling video files.
This model inherits from the File model and provides additional functionality
for reading video files, extracting video frames, and splitting videos into
fragments.
The video_stream_index argument used by video methods is the zero-based
index among video streams, matching FFmpeg v:N and PyAV
container.streams.video[N] selectors.
Source code in datachain/lib/file.py
get_fragment
get_fragment(start: float, end: float) -> VideoFragment
Returns a video fragment from the specified time range.
Parameters:
-
start(float) βThe start time of the fragment in seconds.
-
end(float) βThe end time of the fragment in seconds.
Returns:
-
VideoFragment(VideoFragment) βA Model representing the video fragment.
Source code in datachain/lib/file.py
get_fragments
get_fragments(
duration: float,
start: float = 0,
end: float | None = None,
) -> Iterator[VideoFragment]
Splits the video into multiple fragments of a specified duration.
Parameters:
-
duration(float) βThe duration of each video fragment in seconds.
-
start(float, default:0) βThe starting time in seconds (default: 0).
-
end(float, default:None) βThe ending time in seconds. If None, the entire remaining video is processed (default: None).
Returns:
-
Iterator[VideoFragment]βIterator[VideoFragment]: An iterator yielding video fragments.
Note
If end is not specified, duration will be taken from the video file, which means video metadata needs to be read.
Source code in datachain/lib/file.py
get_frame
get_frame(
frame: int, video_stream_index: int = 0
) -> VideoFrame
Returns a specific video frame by its frame number.
This returns a frame reference without decoding or validating that the
frame exists. The returned timestamp is estimated from FPS metadata.
Pixel access methods decode the requested frame; use get_frames()
for sequential access and decoded frame timestamps when available.
Parameters:
-
frame(int) βThe frame number to read.
-
video_stream_index(int, default:0) βZero-based index among video streams to read. Defaults to 0.
Returns:
-
VideoFrame(VideoFrame) βVideo frame model.
Source code in datachain/lib/file.py
get_frames
get_frames(
start: int = 0,
end: int | None = None,
step: int = 1,
video_stream_index: int = 0,
) -> Iterator[VideoFrame]
Returns video frames from the specified range in the video.
Parameters:
-
start(int, default:0) βThe starting frame number (default: 0).
-
end(int, default:None) βThe ending frame number (exclusive). If None, frames are read until the end of the video (default: None).
-
step(int, default:1) βThe interval between frames to read (default: 1).
-
video_stream_index(int, default:0) βZero-based index among video streams to read. Defaults to 0.
Returns:
-
Iterator[VideoFrame]βIterator[VideoFrame]: An iterator yielding video frames.
Note
If end is not specified, number of frames will be taken from the video file, this means video metadata needs to be read.
Source code in datachain/lib/file.py
get_info
Retrieves metadata and information about the video file.
Metadata is read through File.open(), so it can stream when caching
is disabled. When caching is enabled, opening the file may populate the
local cache first.
Parameters:
-
video_stream_index(int, default:0) βZero-based index among video streams to inspect. Defaults to 0.
Returns:
-
Video(Video) βA Model containing video metadata such as duration, resolution, frame rate, and codec details.
Source code in datachain/lib/file.py
save
Writes its content to destination
Source code in datachain/lib/file.py
VideoFrame
Bases: DataModel
A data model for representing a video frame.
This model inherits from the VideoFile model and adds a frame attribute,
which represents a specific frame within a video file. It allows access
to individual frames and provides functionality for reading and saving
video frames as image files.
Attributes:
-
video(VideoFile) βThe video file containing the video frame.
-
frame(int) βThe frame number referencing a specific frame in the video file.
-
video_stream_index(int) βZero-based index among video streams containing the frame.
-
timestamp(float) βFrame timestamp in seconds. Frames returned by
VideoFile.get_frame()use FPS metadata. Frames yielded byVideoFile.get_frames()use decoded frame timestamps when available.
get_np
get_np() -> ndarray
Returns a video frame from the video file as a NumPy array.
For seekable constant-FPS streams, this seeks near the requested frame
and decodes forward from the previous keyframe. Otherwise, it may decode
from the start. Use VideoFile.get_frames() when reading many frames
sequentially.
Returns:
-
ndarray(ndarray) βA NumPy array representing the video frame, in the shape (height, width, channels).
Source code in datachain/lib/file.py
read_bytes
Returns a video frame from the video file as image bytes.
For seekable constant-FPS streams, this seeks near the requested frame
and decodes forward from the previous keyframe. Otherwise, it may decode
from the start. Use VideoFile.get_frames() when reading many frames
sequentially.
Parameters:
-
format(str, default:'jpg') βThe desired image format (e.g., 'jpg', 'png'). Defaults to 'jpg'.
Returns:
-
bytes(bytes) βThe encoded video frame as image bytes.
Source code in datachain/lib/file.py
save
Saves the current video frame as an image file.
For seekable constant-FPS streams, this seeks near the requested frame
and decodes forward from the previous keyframe. Otherwise, it may decode
from the start. Use VideoFile.get_frames() when reading many frames
sequentially.
If destination is a remote path, the image file will be uploaded
to remote storage.
Parameters:
-
destination(str) βOutput directory path or URI (e.g.
s3://β¦,gs://β¦). -
format(str, default:'jpg') βImage format (e.g., 'jpg', 'png'). Defaults to 'jpg'.
-
client_config(dict | None, default:None) βOptional client configuration (e.g. credentials).
Returns:
-
ImageFile(ImageFile) βA Model representing the saved image file.
Source code in datachain/lib/file.py
VideoFragment
Bases: DataModel
A data model for representing a video fragment.
This model inherits from the VideoFile model and adds start
and end attributes, which represent a specific fragment within a video file.
It allows access to individual fragments and provides functionality for reading
and saving video fragments as separate video files.
Attributes:
-
video(VideoFile) βThe video file containing the video fragment.
-
start(float) βThe starting time of the video fragment in seconds.
-
end(float) βThe ending time of the video fragment in seconds.
save
save(
destination: str,
format: str | None = None,
client_config: dict | None = None,
timeout: float | None = None,
) -> VideoFile
Saves the video fragment as a new video file.
If destination is a remote path, the video file will be uploaded
to remote storage.
Parameters:
-
destination(str) βOutput directory path or URI (e.g.
s3://β¦,gs://β¦). -
format(str | None, default:None) βOutput video format (e.g., 'mp4', 'avi'). If None, inferred from the file extension.
-
client_config(dict | None, default:None) βOptional client configuration (e.g. credentials).
-
timeout(float | None, default:None) βFFmpeg subprocess timeout in seconds. If None, a timeout is computed from the fragment duration. Set to 0 to disable.
Returns:
-
VideoFile(VideoFile) βA Model representing the saved video file.
Source code in datachain/lib/file.py
Video
Bases: DataModel
A data model representing metadata for a video file.
Attributes:
-
width(int) βThe width of the video in pixels. Defaults to -1 if unknown.
-
height(int) βThe height of the video in pixels. Defaults to -1 if unknown.
-
fps(float) βThe frame rate of the video (frames per second). Defaults to -1.0 if unknown.
-
duration(float) βThe total duration of the video in seconds. Defaults to -1.0 if unknown.
-
frames(int) βThe total number of frames in the video. Defaults to -1 if unknown.
-
format(str) βThe format of the video file (e.g., 'mp4', 'avi'). Defaults to an empty string.
-
codec(str) βThe codec used for encoding the video. Defaults to an empty string.