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.