Skip to content

🎥 Make videos programmatically with React

Three.js template: Use Mediabunny for metadata extraction

Jonny Burger9e4a1c3b1740
diff --git a/packages/template-three/package.json b/packages/template-three/package.json
index a5dabe895b..b41d563ce1 100644
--- a/packages/template-three/package.json
+++ b/packages/template-three/package.json
@@ -13,14 +13,14 @@
"dependencies": {
"@react-three/fiber": "9.2.0",
"@remotion/cli": "workspace:*",
- "@remotion/media-utils": "workspace:*",
"@remotion/three": "workspace:*",
"@remotion/zod-types": "workspace:*",
"react": "19.2.3",
"react-dom": "19.2.3",
"remotion": "workspace:*",
"three": "0.178.0",
- "zod": "3.22.3"
+ "zod": "3.22.3",
+ "mediabunny": "1.25.8"
},
"devDependencies": {
"@remotion/eslint-config-flat": "workspace:*",
diff --git a/packages/template-three/src/Scene.tsx b/packages/template-three/src/Scene.tsx
index cc70bde1d2..945daf1d87 100644
--- a/packages/template-three/src/Scene.tsx
+++ b/packages/template-three/src/Scene.tsx
@@ -1,5 +1,4 @@
import { Html5Video, staticFile } from "remotion";
-import { getVideoMetadata, VideoMetadata } from "@remotion/media-utils";
import { ThreeCanvas } from "@remotion/three";
import React, { useEffect, useRef, useState } from "react";
import { AbsoluteFill, useVideoConfig } from "remotion";
@@ -7,6 +6,7 @@ import { Phone } from "./Phone";
import { z } from "zod";
import { zColor } from "@remotion/zod-types";
import { useTexture } from "./use-texture";
+import { getMediaMetadata } from "./helpers/get-media-metadata";
const container: React.CSSProperties = {
backgroundColor: "white",
@@ -31,14 +31,17 @@ export const Scene: React.FC<
> = ({ baseScale, phoneColor, deviceType }) => {
const videoRef = useRef<HTMLVideoElement>(null);
const { width, height } = useVideoConfig();
- const [videoData, setVideoData] = useState<VideoMetadata | null>(null);
+ const [videoData, setVideoData] = useState<{
+ width: number;
+ height: number;
+ } | null>(null);
const videoSrc =
deviceType === "phone" ? staticFile("phone.mp4") : staticFile("tablet.mp4");
useEffect(() => {
- getVideoMetadata(videoSrc)
- .then((data) => setVideoData(data))
+ getMediaMetadata(videoSrc)
+ .then((data) => setVideoData(data.dimensions))
.catch((err) => console.log(err));
}, [videoSrc]);
@@ -60,7 +63,7 @@ export const Scene: React.FC<
phoneColor={phoneColor}
baseScale={baseScale}
videoTexture={texture}
- aspectRatio={videoData.aspectRatio}
+ aspectRatio={videoData.width / videoData.height}
/>
</ThreeCanvas>
) : null}
diff --git a/packages/template-three/src/helpers/get-media-metadata.ts b/packages/template-three/src/helpers/get-media-metadata.ts
new file mode 100644
index 0000000000..6743c135d5
--- /dev/null
+++ b/packages/template-three/src/helpers/get-media-metadata.ts
@@ -0,0 +1,27 @@
+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,
+ };
+};