🎥 Make videos programmatically with React
Upgrade mediabunny to 1.42.0-beta.5 and revert to UrlSource
Jonny Burger947559f416bd
diff --git a/bun.lock b/bun.lockindex d9e329c9a6..c76d02b521 100644--- a/bun.lock+++ b/bun.lock@@ -2501,7 +2501,7 @@"@vitejs/plugin-react": "4.3.4","@vitest/browser-playwright": "4.0.9","eslint": "9.19.0",- "mediabunny": "1.42.0-beta.4",+ "mediabunny": "1.42.0-beta.5","next": "16.2.3","openai": "4.67.1","playwright": "1.55.1",@@ -6408,7 +6408,7 @@"media-typer": ["[email protected]", "", {}, "sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ=="],- "mediabunny": ["[email protected]", "", { "dependencies": { "@types/dom-mediacapture-transform": "^0.1.11", "@types/dom-webcodecs": "0.1.13" } }, "sha512-y4S0KujyBX8mJ+bifVE6dEx5/ZcJlCifhpu5MPQ9lKAtRXOJbv3PFwD3mZgOme4vgXs/YUWBVza9rTKj+SATnQ=="],+ "mediabunny": ["[email protected]", "", { "dependencies": { "@types/dom-mediacapture-transform": "^0.1.11", "@types/dom-webcodecs": "0.1.13" } }, "sha512-UCwHVK1KcLH9aNrb+4wROhVc/2Ryf4mLnCx6vJf4kEbmK/++hxoHQ3rRX/Ac1mEoVRN5CL/0Gwyo1mIZXtV5bA=="],"mem": ["[email protected]", "", { "dependencies": { "map-age-cleaner": "^0.1.1", "mimic-fn": "^2.0.0", "p-is-promise": "^2.0.0" } }, "sha512-qX2bG48pTqYRVmDB37rn/6PT7LcR8T7oAX3bf99u1Tt1nzxYfxkgqDwUwolPlXweM0XzBOBFzSx4kfp7KP1s/w=="],diff --git a/package.json b/package.jsonindex 1452a3299c..ff81a606d4 100644--- a/package.json+++ b/package.json@@ -108,7 +108,7 @@"@types/node": "20.12.14","@types/web": "0.0.166","@types/bun": "1.3.3",- "mediabunny": "1.42.0-beta.4",+ "mediabunny": "1.42.0-beta.5","@mediabunny/mp3-encoder": "1.39.2","@mediabunny/aac-encoder": "1.39.2","@mediabunny/flac-encoder": "1.39.2",diff --git a/packages/convert/app/components/get-supported-configs.tsx b/packages/convert/app/components/get-supported-configs.tsxindex bc74379d26..afcd6f65f6 100644--- a/packages/convert/app/components/get-supported-configs.tsx+++ b/packages/convert/app/components/get-supported-configs.tsx@@ -147,7 +147,7 @@ export const getSupportedConfigs = async ({if (track.isAudioTrack()) {const audioTrackOperations: AudioOperation[] = [];- const canCopy = canCopyAudioTrack({+ const canCopy = await canCopyAudioTrack({outputContainer: container,inputTrack: track,sampleRate,diff --git a/packages/convert/app/components/use-probe.ts b/packages/convert/app/components/use-probe.tsindex b7be6b2d76..753115e7c6 100644--- a/packages/convert/app/components/use-probe.ts+++ b/packages/convert/app/components/use-probe.ts@@ -5,7 +5,7 @@ import type {InputVideoTrack,MetadataTags,} from 'mediabunny';-import {ALL_FORMATS, createInputFrom} from 'mediabunny';+import {ALL_FORMATS, BlobSource, Input, UrlSource} from 'mediabunny';import {useCallback, useEffect, useMemo, useState} from 'react';import type {Dimensions} from '~/lib/calculate-new-dimensions-from-dimensions';import type {Source} from '~/lib/convert-state';@@ -41,10 +41,11 @@ export const useProbe = ({src}: {src: Source}) => {const [error, setError] = useState<Error | null>(null);const input = useMemo(() => {- return createInputFrom(- src.type === 'file' ? src.file : src.url,- ALL_FORMATS,- );+ return new Input({+ formats: ALL_FORMATS,+ source:+ src.type === 'file' ? new BlobSource(src.file) : new UrlSource(src.url),+ });}, [src]);const getStart = useCallback(() => {diff --git a/packages/docs/docs/mediabunny/metadata.mdx b/packages/docs/docs/mediabunny/metadata.mdxindex 20c169ad23..1a04350d17 100644--- a/packages/docs/docs/mediabunny/metadata.mdx+++ b/packages/docs/docs/mediabunny/metadata.mdx@@ -16,11 +16,14 @@ then [Mediabunny](https://mediabunny.dev) is the ideal library for doing so.## Getting metadata from a URL```tsx twoslash title="get-media-metadata.ts"-import {ALL_FORMATS, createInputFrom} from 'mediabunny';+import {Input, ALL_FORMATS, UrlSource} from 'mediabunny';export const getMediaMetadata = async (src: string) => {- const input = createInputFrom(src, ALL_FORMATS, {- getRetryDelay: () => null,+ const input = new Input({+ formats: ALL_FORMATS,+ source: new UrlSource(src, {+ getRetryDelay: () => null,+ }),});const durationInSeconds = await input.computeDuration();@@ -49,16 +52,19 @@ export const getMediaMetadata = async (src: string) => {<p>By setting `getRetryDelay` to `() => null`, we prevent Mediabunny from infinitely retrying if the request fails.</p><p>We compute the duration, the dimensions and the frame rate. If the media file is an audio file, we return `null` for the dimensions and the frame rate.</p><p>To avoid reading the entire file, we at most read 50 packets to compute the frame rate.</p>- <p>`createInputFrom` also supports HLS (`.m3u8`) URLs - it automatically handles the multi-file nature of HLS playlists.</p>+ <p>`UrlSource` also supports HLS (`.m3u8`) URLs - it automatically handles the multi-file nature of HLS playlists.</p></details>```tsx twoslash title="Usage example"// @filename: get-media-metadata.ts-import {ALL_FORMATS, createInputFrom} from 'mediabunny';+import {Input, ALL_FORMATS, UrlSource} from 'mediabunny';export const getMediaMetadata = async (src: string) => {- const input = createInputFrom(src, ALL_FORMATS, {- getRetryDelay: () => null,+ const input = new Input({+ formats: ALL_FORMATS,+ source: new UrlSource(src, {+ getRetryDelay: () => null,+ }),});const durationInSeconds = await input.computeDuration();diff --git a/packages/example/src/get-media-metadata.ts b/packages/example/src/get-media-metadata.tsindex 7a66a4eea0..a60d9b0c8c 100644--- a/packages/example/src/get-media-metadata.ts+++ b/packages/example/src/get-media-metadata.ts@@ -1,8 +1,11 @@-import {ALL_FORMATS, createInputFrom} from 'mediabunny';+import {ALL_FORMATS, Input, UrlSource} from 'mediabunny';export const getMediaMetadata = async (src: string) => {- const input = createInputFrom(src, ALL_FORMATS, {- getRetryDelay: () => null,+ const input = new Input({+ formats: ALL_FORMATS,+ source: new UrlSource(src, {+ getRetryDelay: () => null,+ }),});const durationInSeconds = await input.computeDuration();diff --git a/packages/media-utils/src/use-windowed-audio-data.ts b/packages/media-utils/src/use-windowed-audio-data.tsindex b871cc7ad3..26c9e6e9ef 100644--- a/packages/media-utils/src/use-windowed-audio-data.ts+++ b/packages/media-utils/src/use-windowed-audio-data.ts@@ -1,11 +1,11 @@import type {InputAudioTrack} from 'mediabunny';-import type {Input} from 'mediabunny';import {ALL_FORMATS,+ Input,InputDisposedError,MATROSKA,+ UrlSource,WEBM,- createInputFrom,} from 'mediabunny';import {useCallback,@@ -102,7 +102,10 @@ export const useWindowedAudioData = ({signal.addEventListener('abort', cont, {once: true});- const input = createInputFrom(src, ALL_FORMATS);+ const input = new Input({+ formats: ALL_FORMATS,+ source: new UrlSource(src),+ });const onAbort = () => {input.dispose();diff --git a/packages/media/src/create-input.ts b/packages/media/src/create-input.tsdeleted file mode 100644index 602131165c..0000000000--- a/packages/media/src/create-input.ts+++ /dev/null@@ -1,17 +0,0 @@-import type {CreateInputFromOptions} from 'mediabunny';-import {ALL_FORMATS, createInputFrom} from 'mediabunny';--export const createInput = ({- src,- credentials,- urlSourceOptions,-}: {- src: string;- credentials: RequestCredentials | undefined;- urlSourceOptions?: Partial<CreateInputFromOptions>;-}) => {- return createInputFrom(src, ALL_FORMATS, {- ...urlSourceOptions,- ...(credentials ? {requestInit: {credentials}} : undefined),- });-};diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.tsindex 9ef943eb2f..c47a5ef814 100644--- a/packages/media/src/media-player.ts+++ b/packages/media/src/media-player.ts@@ -1,4 +1,4 @@-import type {Input} from 'mediabunny';+import {ALL_FORMATS, Input, UrlSource} from 'mediabunny';import type {LogLevel, useBufferState} from 'remotion';import {Internals} from 'remotion';import type {ScheduleAudioNodeResult} from 'remotion';@@ -6,7 +6,6 @@ import {audioIteratorManager,type AudioIteratorManager,} from './audio-iterator-manager';-import {createInput} from './create-input';import {drawPreviewOverlay} from './debug-overlay/preview-overlay';import type {DelayPlaybackIfNotPremounting} from './delay-playback-if-not-premounting';import {calculateEndTime, getTimeInSeconds} from './get-time-in-seconds';@@ -136,9 +135,16 @@ export class MediaPlayer {this.onVideoFrameCallback = onVideoFrameCallback;this.playing = playing;this.sequenceOffset = sequenceOffset;- this.input = createInput({- src: this.src,- credentials,+ this.input = new Input({+ source: new UrlSource(+ this.src,+ credentials+ ? {+ requestInit: {credentials},+ }+ : undefined,+ ),+ formats: ALL_FORMATS,});if (canvas) {diff --git a/packages/media/src/video-extraction/get-frames-since-keyframe.ts b/packages/media/src/video-extraction/get-frames-since-keyframe.tsindex 87f0d88b7d..3141ff9a75 100644--- a/packages/media/src/video-extraction/get-frames-since-keyframe.ts+++ b/packages/media/src/video-extraction/get-frames-since-keyframe.ts@@ -1,14 +1,15 @@-import type {Input, InputFormat} from 'mediabunny';-import type {CreateInputFromOptions} from 'mediabunny';+import type {InputFormat, UrlSourceOptions} from 'mediabunny';import {+ ALL_FORMATS,AudioSampleSink,EncodedPacketSink,+ Input,MATROSKA,+ UrlSource,VideoSampleSink,WEBM,} from 'mediabunny';import {canBrowserUseWebGl2} from '../browser-can-use-webgl2';-import {createInput} from '../create-input';import {isNetworkError} from '../is-type-of-error';import {rememberActualMatroskaTimestamps} from './remember-actual-matroska-timestamps';@@ -36,7 +37,7 @@ export type VideoSinkResult =const getRetryDelay = (() => {return null;-}) satisfies CreateInputFromOptions['getRetryDelay'];+}) satisfies UrlSourceOptions['getRetryDelay'];const getFormatOrNullOrNetworkError = async (input: Input,@@ -56,10 +57,12 @@ export const getSinks = async (src: string,credentials: RequestCredentials | undefined,) => {- const input = createInput({- src,- credentials,- urlSourceOptions: {getRetryDelay},+ const input = new Input({+ formats: ALL_FORMATS,+ source: new UrlSource(src, {+ getRetryDelay,+ ...(credentials ? {requestInit: {credentials}} : undefined),+ }),});const format = await getFormatOrNullOrNetworkError(input);diff --git a/packages/studio/src/components/AudioWaveform.tsx b/packages/studio/src/components/AudioWaveform.tsxindex e769852811..93bb79f477 100644--- a/packages/studio/src/components/AudioWaveform.tsx+++ b/packages/studio/src/components/AudioWaveform.tsx@@ -157,6 +157,7 @@ export const AudioWaveform: React.FC<{}, [visualizationWidth, volume, doesVolumeChange]);if (error) {+ // eslint-disable-next-line no-consoleconsole.error(error);return (<div style={container}>diff --git a/packages/studio/src/components/CurrentAsset.tsx b/packages/studio/src/components/CurrentAsset.tsxindex 9199d7490f..f1658c2d36 100644--- a/packages/studio/src/components/CurrentAsset.tsx+++ b/packages/studio/src/components/CurrentAsset.tsx@@ -1,5 +1,5 @@import {formatBytes} from '@remotion/studio-shared';-import {ALL_FORMATS, createInputFrom} from 'mediabunny';+import {ALL_FORMATS, Input, UrlSource} from 'mediabunny';import React, {useContext, useEffect, useMemo, useState} from 'react';import {Internals, staticFile} from 'remotion';import {BACKGROUND, BORDER_COLOR} from '../helpers/colors';@@ -88,7 +88,10 @@ export const CurrentAsset: React.FC = () => {}const url = staticFile(assetName);- const input = createInputFrom(url, ALL_FORMATS);+ const input = new Input({+ formats: ALL_FORMATS,+ source: new UrlSource(url),+ });Promise.all([input.computeDuration(),diff --git a/packages/studio/src/components/load-waveform-peaks.ts b/packages/studio/src/components/load-waveform-peaks.tsindex 8a86e56915..4dc6f06444 100644--- a/packages/studio/src/components/load-waveform-peaks.ts+++ b/packages/studio/src/components/load-waveform-peaks.ts@@ -1,4 +1,4 @@-import {ALL_FORMATS, AudioSampleSink, createInputFrom} from 'mediabunny';+import {ALL_FORMATS, AudioSampleSink, Input, UrlSource} from 'mediabunny';const TARGET_SAMPLE_RATE = 100;@@ -13,7 +13,10 @@ export async function loadWaveformPeaks(const cached = peaksCache.get(url);if (cached) return cached;- const input = createInputFrom(url, ALL_FORMATS);+ const input = new Input({+ formats: ALL_FORMATS,+ source: new UrlSource(url),+ });try {const audioTrack = await input.getPrimaryAudioTrack();diff --git a/packages/studio/src/helpers/extract-frames.ts b/packages/studio/src/helpers/extract-frames.tsindex ae5911a94d..3b2a203bbb 100644--- a/packages/studio/src/helpers/extract-frames.ts+++ b/packages/studio/src/helpers/extract-frames.ts@@ -1,9 +1,10 @@import type {VideoSample} from 'mediabunny';import {ALL_FORMATS,+ Input,InputDisposedError,+ UrlSource,VideoSampleSink,- createInputFrom,} from 'mediabunny';type Options = {@@ -29,7 +30,10 @@ export async function extractFrames({onVideoSample,signal,}: ExtractFramesProps): Promise<void> {- const input = createInputFrom(src, ALL_FORMATS);+ const input = new Input({+ formats: ALL_FORMATS,+ source: new UrlSource(src),+ });const dispose = () => {input.dispose();diff --git a/packages/studio/src/helpers/use-max-media-duration.ts b/packages/studio/src/helpers/use-max-media-duration.tsindex 62320d76ff..26b43f747e 100644--- a/packages/studio/src/helpers/use-max-media-duration.ts+++ b/packages/studio/src/helpers/use-max-media-duration.ts@@ -1,5 +1,5 @@import {getVideoMetadata} from '@remotion/media-utils';-import {ALL_FORMATS, InputDisposedError, createInputFrom} from 'mediabunny';+import {ALL_FORMATS, Input, InputDisposedError, UrlSource} from 'mediabunny';import {useEffect, useState} from 'react';import {type TSequence} from 'remotion';@@ -29,7 +29,10 @@ export const useMaxMediaDuration = (s: TSequence, fps: number) => {return;}- const input = createInputFrom(src, ALL_FORMATS);+ const input = new Input({+ formats: ALL_FORMATS,+ source: new UrlSource(src),+ });input.computeDuration().then((duration) => {diff --git a/packages/template-music-visualization/src/Root.tsx b/packages/template-music-visualization/src/Root.tsxindex 991df3c7cd..a0c31ee366 100644--- a/packages/template-music-visualization/src/Root.tsx+++ b/packages/template-music-visualization/src/Root.tsx@@ -1,4 +1,4 @@-import { ALL_FORMATS, createInputFrom } from "mediabunny";+import { ALL_FORMATS, Input, UrlSource } from "mediabunny";import { Composition, staticFile } from "remotion";import { Visualizer } from "./Visualizer/Main";import { visualizerCompositionSchema } from "./helpers/schema";@@ -35,8 +35,11 @@ export const RemotionRoot: React.FC = () => {}}// Determine the length of the video based on the duration of the audio filecalculateMetadata={async ({ props }) => {- const input = createInputFrom(props.audioFileUrl, ALL_FORMATS, {- getRetryDelay: () => null,+ const input = new Input({+ source: new UrlSource(props.audioFileUrl, {+ getRetryDelay: () => null,+ }),+ formats: ALL_FORMATS,});const durationInSeconds = await input.computeDuration();diff --git a/packages/template-recorder/src/helpers/convert-in-browser.ts b/packages/template-recorder/src/helpers/convert-in-browser.tsindex d6c257a0d0..75edb345f8 100644--- a/packages/template-recorder/src/helpers/convert-in-browser.ts+++ b/packages/template-recorder/src/helpers/convert-in-browser.ts@@ -1,9 +1,10 @@import {ALL_FORMATS,+ BlobSource,BufferTarget,Conversion,+ Input,Output,- createInputFrom,} from "mediabunny";import { getMediabunnyOutput } from "./find-good-supported-codec";@@ -16,7 +17,10 @@ export const convertInBrowser = async ({onProgress: (progress: number, abort: () => void) => void;mimeType: string;}) => {- const input = createInputFrom(src, ALL_FORMATS);+ const input = new Input({+ formats: ALL_FORMATS,+ source: new BlobSource(src),+ });const output = new Output({format: getMediabunnyOutput(mimeType),target: new BufferTarget(),diff --git a/packages/template-three/src/helpers/get-media-metadata.ts b/packages/template-three/src/helpers/get-media-metadata.tsindex 422ab83b0b..e3dbb33393 100644--- a/packages/template-three/src/helpers/get-media-metadata.ts+++ b/packages/template-three/src/helpers/get-media-metadata.ts@@ -1,8 +1,11 @@-import { ALL_FORMATS, createInputFrom } from "mediabunny";+import { ALL_FORMATS, Input, UrlSource } from "mediabunny";export const getMediaMetadata = async (src: string) => {- const input = createInputFrom(src, ALL_FORMATS, {- getRetryDelay: () => null,+ const input = new Input({+ formats: ALL_FORMATS,+ source: new UrlSource(src, {+ getRetryDelay: () => null,+ }),});const durationInSeconds = await input.computeDuration();@@ -11,8 +14,8 @@ export const getMediaMetadata = async (src: string) => {throw new Error(`Video track not found in source: ${src}`);}const dimensions = {- width: await videoTrack.getDisplayWidth(),- height: await videoTrack.getDisplayHeight(),+ width: videoTrack.displayWidth,+ height: videoTrack.displayHeight,};const packetStats = await videoTrack.computePacketStats(50);const fps = packetStats?.averagePacketRate ?? null;