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.VideoFragment
- represents a fragment of a video file.
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.
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: Optional[float] = 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, number of frames will be taken from the video file, this means video file needs to be downloaded.
Source code in datachain/lib/file.py
get_frame
get_frame(frame: int) -> VideoFrame
Returns a specific video frame by its frame number.
Parameters:
-
frame
(int
) โThe frame number to read.
Returns:
-
VideoFrame
(VideoFrame
) โVideo frame model.
Source code in datachain/lib/file.py
get_frames
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).
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 file needs to be downloaded.
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.
get_np
get_np() -> ndarray
Returns a video frame from the video file as a NumPy array.
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.
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.
If output
is a remote path, the image file will be uploaded to remote storage.
Parameters:
-
output
(str
) โThe destination path, which can be a local file path or a remote URL.
-
format
(str
, default:'jpg'
) โThe image format (e.g., 'jpg', 'png'). Defaults to 'jpg'.
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
Saves the video fragment as a new video file.
If output
is a remote path, the video file will be uploaded to remote storage.
Parameters:
-
output
(str
) โThe destination path, which can be a local file path or a remote URL.
-
format
(str
, default:None
) โThe output video format (e.g., 'mp4', 'avi'). If None, the format is inferred from the file extension.
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.