Skip to main content
Record the R3F canvas output to a video file using the MediaRecorder API.

Signature

useVideoRecorder(options?: {
  fps?: number;
  mimeType?: string;
}): {
  start: () => void;
  stop: () => Promise<Blob>;
  download: (filename?: string) => void;
  recording: boolean;
}

Usage

import { useVideoRecorder } from "mujoco-react";

function VideoControls() {
  const video = useVideoRecorder({ fps: 30 });

  return (
    <div>
      {video.recording ? (
        <button onClick={async () => {
          await video.stop();
          video.download("simulation.webm");
        }}>
          Stop Recording
        </button>
      ) : (
        <button onClick={video.start}>Record Video</button>
      )}
    </div>
  );
}

Options

FieldTypeDefaultDescription
fpsnumber30Target frame rate
mimeTypestring'video/webm'Video container format

Return Value

FieldTypeDescription
start() => voidBegin recording
stop() => Promise<Blob>Stop recording and return the video blob
download(filename?: string) => voidDownload the last recorded video
recordingbooleanWhether currently recording

Supported Formats

MIME TypeExtensionBrowser Support
video/webm.webmChrome, Firefox, Edge
video/webm;codecs=vp9.webmChrome, Edge
video/mp4.mp4Safari (limited)

Notes

  • Uses the MediaRecorder API on the canvas stream
  • Recording quality depends on canvas resolution and browser encoding
  • stop() returns a Promise since the MediaRecorder needs to finalize the file
  • WebM is the most widely supported format for canvas recording