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.
With prefetch and cache off, reads only the parts of the file it needs.
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
save(
destination: str,
client_config: dict | None = None,
*,
content_type: str | None = None,
content_disposition: str | None = None,
cache_control: str | None = None,
content_encoding: str | None = None,
metadata: dict[str, str] | None = None,
write_options: dict[str, Any] | None = None
) -> VideoFile
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.
Note
The decoded av.VideoFrame is cached in the _decoded instance
attribute the first time any of get_np(), read_bytes(), or
save() is called. Frames yielded by VideoFile.get_frames()
are pre-populated with the decoded frame, so the first call to any of
those methods requires no I/O. The cache is held for the lifetime of
the VideoFrame object; when many frames are kept in memory at once
(e.g. list(video.get_frames())) the cached pixel buffers can
create significant memory pressure (~3 MB each for 1080p video). The
cache is excluded from pickling so cross-process serialisation (e.g.
DataChain .gen() workers) is unaffected.
Source code in datachain/lib/file.py
get_np
get_np() -> ndarray
Returns a video frame from the video file as a NumPy array.
The decoded av.VideoFrame is cached in _decoded on the first
call, so repeated calls to get_np() return the cached pixels
without a second decode pass regardless of whether the frame came
from VideoFile.get_frames() or VideoFile.get_frame().
Display rotation is applied (matching FFmpeg/OpenCV), so a 90/270 rotation swaps width and height. Channels are RGB; consumers expecting BGR (e.g. OpenCV, Ultralytics) must convert.
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.
Uses the cached decoded frame when available (e.g. after get_np()
or for frames yielded by VideoFile.get_frames()), avoiding a second
decode pass.
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
save(
destination: str | PathLike[str],
format: str = "jpg",
client_config: dict | None = None,
*,
content_type: str | None = None,
content_disposition: str | None = None,
cache_control: str | None = None,
content_encoding: str | None = None,
metadata: dict[str, str] | None = None,
write_options: dict[str, Any] | None = None
) -> ImageFile
Saves the current video frame as an image file.
Uses the cached decoded frame when available (e.g. after get_np()
or for frames yielded by VideoFile.get_frames()), avoiding a second
decode pass.
If destination is a remote path, the image file will be uploaded
to remote storage.
Parameters:
-
destination(str | PathLike[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).
-
content_type(str | None, default:None) βContent-Typeto set on the written object. -
content_disposition(str | None, default:None) βContent-Dispositionto set on the object. -
cache_control(str | None, default:None) βCache-Controlto set on the object. -
content_encoding(str | None, default:None) βContent-Encodingto set on the object. -
metadata(dict[str, str] | None, default:None) βCustom key/value metadata to attach to the object.
-
write_options(dict[str, Any] | None, default:None) βRaw, backend-native write kwargs merged after the normalized fields above.
Returns:
-
ImageFile(ImageFile) βA Model representing the saved image file.
Source code in datachain/lib/file.py
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 | |
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,
*,
content_type: str | None = None,
content_disposition: str | None = None,
cache_control: str | None = None,
content_encoding: str | None = None,
metadata: dict[str, str] | None = None,
write_options: dict[str, Any] | 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.
The output is named {stem}_{hash}_{start_ms}_{end_ms}.{format}, where
hash is derived from the source video's source and path, so
videos with the same name in different directories don't overwrite each
other.
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.
-
content_type(str | None, default:None) βContent-Typeto set on the written object. -
content_disposition(str | None, default:None) βContent-Dispositionto set on the object. -
cache_control(str | None, default:None) βCache-Controlto set on the object. -
content_encoding(str | None, default:None) βContent-Encodingto set on the object. -
metadata(dict[str, str] | None, default:None) βCustom key/value metadata to attach to the object.
-
write_options(dict[str, Any] | None, default:None) βRaw, backend-native write kwargs merged after the normalized fields above.
Returns:
-
VideoFile(VideoFile) βA Model representing the saved video file.
Source code in datachain/lib/file.py
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 | |
Video
Bases: DataModel
A data model representing metadata for a video file.
Attributes:
-
width(int) βDisplay width in pixels (after stream rotation). Defaults to -1 if unknown.
-
height(int) βDisplay height in pixels (after stream rotation). 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.