Skip to content

🎥 Make videos programmatically with React

new skills for mediabunny

Jonny Burgere44b6d952dcd
diff --git a/packages/skills/skills/mediabunny/can-decode/SKILL.md b/packages/skills/skills/mediabunny/can-decode/SKILL.md
new file mode 100644
index 0000000000..b7146c9caf
--- /dev/null
+++ b/packages/skills/skills/mediabunny/can-decode/SKILL.md
@@ -0,0 +1,75 @@
+---
+name: can-decode
+description: Check if a video can be decoded by the browser using Mediabunny
+metadata:
+ tags: decode, validation, video, audio, compatibility, browser
+---
+
+# Checking if a video can be decoded
+
+Use Mediabunny to check if a video can be decoded by the browser before attempting to play it.
+
+## The `canDecode()` function
+
+This function can be copy-pasted into any project.
+
+```tsx
+import { Input, ALL_FORMATS, UrlSource } from "mediabunny";
+
+export const canDecode = async (src: string) => {
+ const input = new Input({
+ formats: ALL_FORMATS,
+ source: new UrlSource(src, {
+ getRetryDelay: () => null,
+ }),
+ });
+
+ try {
+ await input.getFormat();
+ } catch {
+ return false;
+ }
+
+ const videoTrack = await input.getPrimaryVideoTrack();
+ if (videoTrack && !(await videoTrack.canDecode())) {
+ return false;
+ }
+
+ const audioTrack = await input.getPrimaryAudioTrack();
+ if (audioTrack && !(await audioTrack.canDecode())) {
+ return false;
+ }
+
+ return true;
+};
+```
+
+## Usage
+
+```tsx
+const src = "https://remotion.media/video.mp4";
+const isDecodable = await canDecode(src);
+
+if (isDecodable) {
+ console.log("Video can be decoded");
+} else {
+ console.log("Video cannot be decoded by this browser");
+}
+```
+
+## Using with Blob
+
+For file uploads or drag-and-drop, use `BlobSource`:
+
+```tsx
+import { Input, ALL_FORMATS, BlobSource } from "mediabunny";
+
+export const canDecodeBlob = async (blob: Blob) => {
+ const input = new Input({
+ formats: ALL_FORMATS,
+ source: new BlobSource(blob),
+ });
+
+ // Same validation logic as above
+};
+```
diff --git a/packages/skills/skills/mediabunny/extract-frames/SKILL.md b/packages/skills/skills/mediabunny/extract-frames/SKILL.md
new file mode 100644
index 0000000000..46e63efc58
--- /dev/null
+++ b/packages/skills/skills/mediabunny/extract-frames/SKILL.md
@@ -0,0 +1,229 @@
+---
+name: extract-frames
+description: Extract frames from videos at specific timestamps using Mediabunny
+metadata:
+ tags: frames, extract, video, thumbnail, filmstrip, canvas
+---
+
+# Extracting frames from videos
+
+Use Mediabunny to extract frames from videos at specific timestamps. This is useful for generating thumbnails, filmstrips, or processing individual frames.
+
+## The `extractFrames()` function
+
+This function can be copy-pasted into any project.
+
+```tsx
+import {
+ ALL_FORMATS,
+ Input,
+ UrlSource,
+ VideoSample,
+ VideoSampleSink,
+} from "mediabunny";
+
+type Options = {
+ track: { width: number; height: number };
+ container: string;
+ durationInSeconds: number | null;
+};
+
+export type ExtractFramesTimestampsInSecondsFn = (
+ options: Options
+) => Promise<number[]> | number[];
+
+export type ExtractFramesProps = {
+ src: string;
+ timestampsInSeconds: number[] | ExtractFramesTimestampsInSecondsFn;
+ onVideoSample: (sample: VideoSample) => void;
+ signal?: AbortSignal;
+};
+
+export async function extractFrames({
+ src,
+ timestampsInSeconds,
+ onVideoSample,
+ signal,
+}: ExtractFramesProps): Promise<void> {
+ using input = new Input({
+ formats: ALL_FORMATS,
+ source: new UrlSource(src),
+ });
+
+ const [durationInSeconds, format, videoTrack] = await Promise.all([
+ input.computeDuration(),
+ input.getFormat(),
+ input.getPrimaryVideoTrack(),
+ ]);
+
+ if (!videoTrack) {
+ throw new Error("No video track found in the input");
+ }
+
+ if (signal?.aborted) {
+ throw new Error("Aborted");
+ }
+
+ const timestamps =
+ typeof timestampsInSeconds === "function"
+ ? await timestampsInSeconds({
+ track: {
+ width: videoTrack.displayWidth,
+ height: videoTrack.displayHeight,
+ },
+ container: format.name,
+ durationInSeconds,
+ })
+ : timestampsInSeconds;
+
+ if (timestamps.length === 0) {
+ return;
+ }
+
+ if (signal?.aborted) {
+ throw new Error("Aborted");
+ }
+
+ const sink = new VideoSampleSink(videoTrack);
+
+ for await (using videoSample of sink.samplesAtTimestamps(timestamps)) {
+ if (signal?.aborted) {
+ break;
+ }
+
+ if (!videoSample) {
+ continue;
+ }
+
+ onVideoSample(videoSample);
+ }
+}
+```
+
+## Basic usage
+
+Extract frames at specific timestamps:
+
+```tsx
+await extractFrames({
+ src: "https://remotion.media/video.mp4",
+ timestampsInSeconds: [0, 1, 2, 3, 4],
+ onVideoSample: (sample) => {
+ const canvas = document.createElement("canvas");
+ canvas.width = sample.displayWidth;
+ canvas.height = sample.displayHeight;
+ const ctx = canvas.getContext("2d");
+ sample.draw(ctx!, 0, 0);
+ },
+});
+```
+
+## Creating a filmstrip
+
+Use a callback function to dynamically calculate timestamps based on video metadata:
+
+```tsx
+const canvasWidth = 500;
+const canvasHeight = 80;
+const fromSeconds = 0;
+const toSeconds = 10;
+
+await extractFrames({
+ src: "https://remotion.media/video.mp4",
+ timestampsInSeconds: async ({ track, durationInSeconds }) => {
+ const aspectRatio = track.width / track.height;
+ const amountOfFramesFit = Math.ceil(
+ canvasWidth / (canvasHeight * aspectRatio)
+ );
+ const segmentDuration = toSeconds - fromSeconds;
+ const timestamps: number[] = [];
+
+ for (let i = 0; i < amountOfFramesFit; i++) {
+ timestamps.push(
+ fromSeconds + (segmentDuration / amountOfFramesFit) * (i + 0.5)
+ );
+ }
+
+ return timestamps;
+ },
+ onVideoSample: (sample) => {
+ console.log(`Frame at ${sample.timestamp}s`);
+
+ const canvas = document.createElement("canvas");
+ canvas.width = sample.displayWidth;
+ canvas.height = sample.displayHeight;
+ const ctx = canvas.getContext("2d");
+ sample.draw(ctx!, 0, 0);
+ },
+});
+```
+
+## Cancellation with AbortSignal
+
+Cancel frame extraction after a timeout:
+
+```tsx
+const controller = new AbortController();
+
+setTimeout(() => controller.abort(), 5000);
+
+try {
+ await extractFrames({
+ src: "https://remotion.media/video.mp4",
+ timestampsInSeconds: [0, 1, 2, 3, 4],
+ onVideoSample: (sample) => {
+ using frame = sample;
+ const canvas = document.createElement("canvas");
+ canvas.width = frame.displayWidth;
+ canvas.height = frame.displayHeight;
+ const ctx = canvas.getContext("2d");
+ frame.draw(ctx!, 0, 0);
+ },
+ signal: controller.signal,
+ });
+
+ console.log("Frame extraction complete!");
+} catch (error) {
+ console.error("Frame extraction was aborted or failed:", error);
+}
+```
+
+## Timeout with Promise.race
+
+```tsx
+const controller = new AbortController();
+
+const timeoutPromise = new Promise<never>((_, reject) => {
+ const timeoutId = setTimeout(() => {
+ controller.abort();
+ reject(new Error("Frame extraction timed out after 10 seconds"));
+ }, 10000);
+
+ controller.signal.addEventListener("abort", () => clearTimeout(timeoutId), {
+ once: true,
+ });
+});
+
+try {
+ await Promise.race([
+ extractFrames({
+ src: "https://remotion.media/video.mp4",
+ timestampsInSeconds: [0, 1, 2, 3, 4],
+ onVideoSample: (sample) => {
+ using frame = sample;
+ const canvas = document.createElement("canvas");
+ canvas.width = frame.displayWidth;
+ canvas.height = frame.displayHeight;
+ const ctx = canvas.getContext("2d");
+ frame.draw(ctx!, 0, 0);
+ },
+ signal: controller.signal,
+ }),
+ timeoutPromise,
+ ]);
+
+ console.log("Frame extraction complete!");
+} catch (error) {
+ console.error("Frame extraction was aborted or failed:", error);
+}
+```
diff --git a/packages/skills/skills/mediabunny/metadata/SKILL.md b/packages/skills/skills/mediabunny/metadata/SKILL.md
new file mode 100644
index 0000000000..ded270e888
--- /dev/null
+++ b/packages/skills/skills/mediabunny/metadata/SKILL.md
@@ -0,0 +1,79 @@
+---
+name: metadata
+description: Getting video duration, dimensions, and frame rate with Mediabunny
+metadata:
+ tags: metadata, duration, width, height, fps, video, audio
+---
+
+# Getting video metadata with Mediabunny
+
+Mediabunny can extract metadata from video and audio files including duration, dimensions, and frame rate. It works in browser, Node.js, and Bun environments without DOM manipulation.
+
+## Getting metadata
+
+```tsx
+import { Input, ALL_FORMATS, UrlSource } from "mediabunny";
+
+export const getMediaMetadata = async (src: string) => {
+ const input = new Input({
+ formats: ALL_FORMATS,
+ source: new UrlSource(src, {
+ getRetryDelay: () => null,
+ }),
+ });
+
+ const durationInSeconds = await input.computeDuration();
+ const videoTrack = await input.getPrimaryVideoTrack();
+ const dimensions = videoTrack
+ ? {
+ width: videoTrack.displayWidth,
+ height: videoTrack.displayHeight,
+ }
+ : null;
+ const packetStats = await videoTrack?.computePacketStats(50);
+ const fps = packetStats?.averagePacketRate ?? null;
+
+ return {
+ durationInSeconds,
+ dimensions,
+ fps,
+ };
+};
+```
+
+## Usage
+
+```tsx
+const metadata = await getMediaMetadata("https://remotion.media/video.mp4");
+
+console.log(metadata.durationInSeconds); // e.g. 10.5
+console.log(metadata.dimensions); // e.g. { width: 1920, height: 1080 }
+console.log(metadata.fps); // e.g. 30
+```
+
+## Return value
+
+- `durationInSeconds`: Duration of the media in seconds
+- `dimensions`: Object with `width` and `height` (null for audio-only files)
+- `fps`: Frame rate (null for audio-only files)
+
+## Using with local files
+
+For local files, use `FileSource` instead of `UrlSource`:
+
+```tsx
+import { Input, ALL_FORMATS, FileSource } from "mediabunny";
+
+const input = new Input({
+ formats: ALL_FORMATS,
+ source: new FileSource(file), // File object from input or drag-drop
+});
+```
+
+## Using with staticFile in Remotion
+
+```tsx
+import { staticFile } from "remotion";
+
+const metadata = await getMediaMetadata(staticFile("video.mp4"));
+```