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 URLBlob— Raw video dataFile— 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 sourceisLoading(boolean):truewhile frame extraction is runningerror(Error | null): Error state if extraction failsclearError(() => void): Clear any existing error
extractFrame Input
source(string | Blob | File | { url: string }, required): Video sourcetime(number |"first"|"last"): Frame positionformat("image/png" | "image/jpeg"): Output image formatquality(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
- Client-side extraction — Uses the browser's native video and canvas APIs, so no server API is required
- Reusable file output — The returned
Filecan be passed directly to APIs likeuseSubmitVideoJob - No automatic upload — Extraction stays local unless your app explicitly uploads the file
- Flexible input shapes — Accepts URLs,
Blobs,Files, and{ url }objects - Automatic error handling — Errors always toast.
extractFrameresolves toundefinedinstead of rejecting.
Best Practices
- Use
time: "last"when chaining one generated video into the next - Use the returned
Filedirectly when feeding the frame intouseSubmitVideoJob - Use
URL.createObjectURL(frameFile)when you want a local preview - Use
useFileUploadif you want to persist the extracted frame - Prefer
"image/png"unless you specifically want a smaller JPEG file - Keep timestamp values within the video duration for predictable results
Error Handling
const frameFile = await extractFrame({
source: videoUrl,
time: "last",
});
if (!frameFile) return;