MixtrainDocsBlog
from mixtrain import Video

Overview

Use Video for video inputs and outputs. Mixtrain displays returned videos in a player, and you can download them locally for frame-by-frame processing.

Constructor

Video(
    url: str,
    *,
    content_type: str | None = None,
    filename: str | None = None,
    duration_seconds: float | None = None,
    width: int | None = None,
    height: int | None = None,
    format: str | None = None,
)
ParameterTypeDescription
urlstrWorkspace path or URL (https://, gs://, s3://)
content_typestr | NoneMIME type
filenamestr | NoneOriginal filename
duration_secondsfloat | NoneDuration in seconds
widthint | NoneWidth in pixels
heightint | NoneHeight in pixels
formatstr | NoneFormat hint (mp4, webm)
video = Video(
    "gallery/video.mp4",
    width=1920,
    height=1080,
    duration_seconds=10.5,
    format="mp4"
)

Properties

PropertyTypeDescription
urlstrWorkspace path or remote URL
content_typestr | NoneMIME type
filenamestr | NoneOriginal filename
duration_secondsfloat | NoneDuration in seconds
widthint | NoneWidth in pixels
heightint | NoneHeight in pixels
formatstr | NoneFormat (mp4, webm)

Methods

to_file()

Inherited from File. Downloads the video and returns the local path. Cached across calls.

path = video.to_file()

to_cv2()

Open the video as an OpenCV VideoCapture.

video.to_cv2() -> cv2.VideoCapture

Downloads the video if needed and opens it with OpenCV.

import cv2

cap = video.to_cv2()
ret, frame = cap.read()
if ret:
    cv2.imshow("First frame", frame)

to_frames()

Iterate over video frames as numpy arrays.

video.to_frames() -> Iterator[np.ndarray]

Yields each frame as a numpy array in (H, W, C) BGR format.

for i, frame in enumerate(video.to_frames()):
    print(f"Frame {i}: {frame.shape}")
    if i >= 10:
        break

Using Video

You can use Video in your models, datasets, workflows, and routines.

As output

from mixtrain import MixModel, Video

class VideoGenerator(MixModel):
    def run(self, inputs=None):
        video_url = self._generate(inputs["prompt"])
        return {
            "result": Video(
                url=video_url,
                width=1920,
                height=1080,
                duration_seconds=5.0
            )
        }

As input

from mixtrain import MixModel, Video

class VideoAnalyzer(MixModel):
    def run(self, video: Video):
        for frame in video.to_frames():
            # Process each frame
            pass
        return {"frames_processed": 150}

From model result

from mixtrain import Model

model = Model("hunyuan-video")
result = model.run({"prompt": "A cat playing"})

print(result.video.url)
print(f"Duration: {result.video.duration_seconds}s")

Storing Videos

Use Video.from_file() for a local video, then call save() to persist it to workspace storage:

video = Video.from_file("demo.mp4", duration_seconds=10.5)
saved_video = video.save("gallery/demo.mp4")

On this page