Video Frame Extraction

Extract still frames from videos using the useVideoFrame hook.

Important: Like all React hooks, useVideoFrame must only be called at the top level of React function components — not inside loops, conditions, or nested functions.

Import

import { useVideoFrame } from "@/hooks/use-video-frame";

When to Use

Use useVideoFrame for:

  • Extracting the last frame from a generated video to start the next shot
  • Capturing the first frame from an uploaded or generated video
  • Grabbing a frame at a specific timestamp for thumbnails or previews
  • Converting video moments into still images that can be passed to useSubmitVideoJob

Supported Video Sources

The hook accepts these video source types:

  • string — A video URL
  • Blob — Raw video data
  • File — A local video file
  • { url: string } — URL object shape for consistency with other platform APIs

API Reference

Returns

  • extractFrame ((params) => Promise<File | undefined>): Extract a frame from a video source
  • isLoading (boolean): true while frame extraction is running
  • error (Error | null): Error state if extraction fails
  • clearError (() => void): Clear any existing error

extractFrame Input

  • source (string | Blob | File | { url: string }, required): Video source
  • time (number | "first" | "last"): Frame position
  • format ("image/png" | "image/jpeg"): Output image format
  • quality (number): Image quality

extractFrame Return Value

Returns a File containing the extracted image, or undefined when extraction fails.

Example: Extract the Last Frame from a Generated Video

import * as React from "react";
import { useVideoFrame } from "@/hooks/use-video-frame";

export function LastFramePreview({ videoUrl }: { videoUrl: string }) {
  const { extractFrame, isLoading } = useVideoFrame();
  const [frameUrl, setFrameUrl] = React.useState<string | null>(null);

  const handleExtract = async () => {
    const frameFile = await extractFrame({
      source: videoUrl,
      time: "last",
    });

    if (frameFile) {
      setFrameUrl(URL.createObjectURL(frameFile));
    }
  };

  return (
    <div>
      <button onClick={handleExtract} disabled={isLoading}>
        Extract last frame
      </button>
      {frameUrl ? <img src={frameUrl} alt="Extracted frame" /> : null}
    </div>
  );
}

Example: Iterative Video Workflow

Use the extracted frame file as the first frame for the next video generation step.

import { useSubmitVideoJob, useVideoJobStatus } from "@/hooks/use-ai";
import { usePersistentItem } from "@/hooks/use-persistent-item";
import { useVideoFrame } from "@/hooks/use-video-frame";

export function ContinueVideo({ sourceVideoUrl }: { sourceVideoUrl: string }) {
  const [jobId, setJobId] = usePersistentItem<string | null>(
    "continuedVideoJobId",
    null,
  );
  const [videoUrl, setVideoUrl] = usePersistentItem<string | null>(
    "continuedVideoUrl",
    null,
  );
  const { submitVideo, isSubmitting } = useSubmitVideoJob();
  const { extractFrame } = useVideoFrame();

  const job = useVideoJobStatus(jobId, {
    onComplete: ({ url }) => {
      setVideoUrl(url);
      setJobId(null);
    },
    onError: () => setJobId(null),
  });

  const handleContinue = async () => {
    const lastFrameFile = await extractFrame({
      source: sourceVideoUrl,
      time: "last",
    });
    if (!lastFrameFile) return;

    const result = await submitVideo({
      prompt: "A bird flies across the lake and the sun grows brighter",
      firstFrame: lastFrameFile,
      aspectRatio: "16:9",
      durationSeconds: 4,
    });
    if (result) {
      setJobId(result.jobId);
    }
  };

  const isGenerating = job.status === "pending" || job.status === "processing";

  return (
    <div>
      <button onClick={handleContinue} disabled={isSubmitting || isGenerating}>
        Continue video
      </button>
      {videoUrl ? <video src={videoUrl} controls /> : null}
    </div>
  );
}

Example: Extract a Frame at a Specific Timestamp

const { extractFrame } = useVideoFrame();

const frameFile = await extractFrame({
  source: { url: videoUrl },
  time: 1.5,
  format: "image/jpeg",
  quality: 0.9,
});

Example: Extract the First Frame from a Local File

const { extractFrame } = useVideoFrame();

const handleFile = async (file: File) => {
  const frameFile = await extractFrame({
    source: file,
    time: "first",
  });

  if (frameFile) {
    setPreviewUrl(URL.createObjectURL(frameFile));
  }
};

Key Behaviors

  1. Client-side extraction — Uses the browser's native video and canvas APIs, so no server API is required
  2. Reusable file output — The returned File can be passed directly to APIs like useSubmitVideoJob
  3. No automatic upload — Extraction stays local unless your app explicitly uploads the file
  4. Flexible input shapes — Accepts URLs, Blobs, Files, and { url } objects
  5. Automatic error handling — Errors always toast. extractFrame resolves to undefined instead of rejecting.

Best Practices

  1. Use time: "last" when chaining one generated video into the next
  2. Use the returned File directly when feeding the frame into useSubmitVideoJob
  3. Use URL.createObjectURL(frameFile) when you want a local preview
  4. Use useFileUpload if you want to persist the extracted frame
  5. Prefer "image/png" unless you specifically want a smaller JPEG file
  6. Keep timestamp values within the video duration for predictable results

Error Handling

const frameFile = await extractFrame({
  source: videoUrl,
  time: "last",
});

if (!frameFile) return;