Video Stitching
Concatenate multiple video clips into a single video using the useVideoStitch hook.
Important: Like all React hooks, useVideoStitch must only be called at the top level of React function components — not inside loops, conditions, or nested functions.
Import
import { useVideoStitch } from "@/hooks/use-video-stitch";
When to Use
Use useVideoStitch for:
- Joining two or more AI-generated video clips into one continuous video
- Combining shots from a multi-step generation workflow into a final cut
- Assembling a sequence of clips so the user can download a single file
Supported Video Sources
The hook accepts these video source types for each clip:
string— A video URLBlob— Raw video dataFile— A local video file{ url: string }— URL object shape for consistency with other platform APIs
All URL-based sources must be same-origin or CORS-enabled. Videos stored on the platform (from useVideoJobStatus or useFileUpload) work automatically.
API Reference
Returns
stitch((params) => Promise<File | undefined>): Concatenate video sources into a single video fileisProcessing(boolean):truewhile a stitch operation is runningprogress(number): Progress from 0 to 1 for the current operationerror(Error | null): Error state if stitching failscancel(() => void): Cancel a running stitch operationclearError(() => void): Clear any existing error
stitch Input
sources(Array<string | Blob | File | { url: string }>, required): Two or more video sources to concatenate in orderwidth(number): Output width in pixels; defaults to the first video's native widthheight(number): Output height in pixels; defaults to the first video's native heightmuted(boolean, default:false): Whether to discard all source audio
When clips must be re-encoded (incompatible codecs or dimensions), the stitcher uses each clip's probed source bitrate when available, otherwise a fixed quality preset (CRF).
stitch Return Value
The returned File contains an MP4 video. Its name property already includes the .mp4 extension, so you can pass it directly to useDownload without specifying a filename. It also works with useFileUpload or any API that accepts File or Blob.
Example: Stitch Completed Videos
import { useVideoStitch } from "@/hooks/use-video-stitch";
import { useDownload } from "@/hooks/use-download";
export function StitchedVideoDownload({ clipUrls }: { clipUrls: string[] }) {
const { stitch, isProcessing, progress } = useVideoStitch();
const { download } = useDownload();
const handleStitch = async () => {
const result = await stitch({
sources: clipUrls.map((url) => ({ url })),
});
if (result) {
download(result);
}
};
return (
<div>
<button
onClick={handleStitch}
disabled={isProcessing || clipUrls.length < 2}
>
Stitch and download
</button>
{isProcessing ? <progress value={progress * 100} /> : null}
</div>
);
}
Example: Stitch Without Audio
const result = await stitch({
sources: [clip1Url, clip2Url],
muted: true,
});
For an iterative workflow, use useVideoFrame to extract the last frame, submit
the continuation with useSubmitVideoJob, and wait for useVideoJobStatus to
complete. Then pass both completed video URLs to StitchedVideoDownload.
Key Behaviors
- Client-side processing — Processes media locally in the browser, so no server round-trip is required. The first operation may load browser-side processing assets
- Audio preserved by default — Original audio from each clip is included in the output. Pass
muted: trueto discard audio - Processing time varies — Processing depends on clip length, resolution, codec compatibility, and device performance. Compatible clips may finish quickly; clips that need normalization can take longer
- Reusable file output — The returned
Filecan be passed directly touseDownload(filename is optional; the file already has the correct name and extension),useFileUpload, or other platform APIs - Progress tracking — The
progressvalue (0 to 1) updates as each clip is processed - Cancellable — Call
cancel()to abort a running stitch operation - Automatic error handling — Errors always toast.
stitchresolves toundefinedinstead of rejecting. - Output format — Produces an MP4
File. The returnedFilealready includes the.mp4extension in its name, so pass it todownload(result)without hardcoding a file extension - Exclusive processing — Only one video stitch or audio overlay operation can run at a time
Best Practices
- Generate all clips before calling
stitchso the user sees generation progress separately from stitching progress - Use the
progressvalue to show a progress bar during stitching - Use
useDownloadto let the user save the stitched result - Use
useFileUploadif you want to persist the stitched video to the platform - All clips should have the same aspect ratio for best results; mismatched clips will be scaled to fit the output dimensions
- Wait for the current video utility operation to finish before starting another stitch or audio overlay
Error Handling
const result = await stitch({
sources: [videoUrl1, videoUrl2],
});
if (!result) return;
Common errors:
"At least two video sources are required."— Thesourcesarray must contain 2 or more items"Another video operation is already running."— Wait for the current stitch or audio overlay operation to finish before starting another"Failed to fetch media source (...)."— A media URL could not be fetched (check CORS, URL validity, and network access)"Failed to load video."— The browser could not load video metadata for a source"Output dimensions must be positive numbers."—widthandheight, when provided, must be positive"Video operation was cancelled."or"Media load was cancelled."— The operation was cancelled viacancel()