🎥 Make videos programmatically with React
Persist media metadata so timeline clips appear before it is fetched
JonnyBurger100abab6ef99
diff --git a/packages/studio/src/helpers/use-media-metadata.ts b/packages/studio/src/helpers/use-media-metadata.tsindex bbaf42e7c5..bfcbed1fc6 100644--- a/packages/studio/src/helpers/use-media-metadata.ts+++ b/packages/studio/src/helpers/use-media-metadata.ts@@ -1,4 +1,8 @@import {getVideoMetadata} from '@remotion/media-utils';+import {+ readMediaMetadataFromPersistentCache,+ writeMediaMetadataToPersistentCache,+} from '@remotion/timeline-utils';import type {FrameRateMetrics,InputAudioTrack,@@ -9,6 +13,8 @@ import {useEffect, useState} from 'react';import {Internals} from 'remotion';import {getDurationOrCompute} from './get-duration-or-compute';+// Persisted by the timeline cache of @remotion/timeline-utils. Bump its+// SCHEMA_VERSION when changing this type.export type MediaMetadata = {duration: number;format: string | null;@@ -185,9 +191,23 @@ export const getMediaMetadata = (return pendingRequest;}- const request = getMediabunnyMetadata(src)- .catch(() => null)- .then((metadata) => metadata ?? getFallbackVideoMetadata(src))+ // The timeline can only draw a media clip once its duration is known, so+ // metadata from a previous page load avoids waiting for the network.+ const request = readMediaMetadataFromPersistentCache(src)+ .then(async (persisted) => {+ if (persisted) {+ return persisted as MediaMetadata;+ }++ const metadata =+ (await getMediabunnyMetadata(src).catch(() => null)) ??+ (await getFallbackVideoMetadata(src));+ if (metadata) {+ writeMediaMetadataToPersistentCache({src, mediaMetadata: metadata});+ }++ return metadata;+ }).then((metadata) => {if (metadata) {cache.set(src, metadata);diff --git a/packages/timeline-utils/src/index.ts b/packages/timeline-utils/src/index.tsindex 4e445f9df5..4386b1bad0 100644--- a/packages/timeline-utils/src/index.ts+++ b/packages/timeline-utils/src/index.ts@@ -46,6 +46,8 @@ export type {LoopDisplaySegment, TimelineLoopDisplay} from './loop-display';export {clearPersistentTimelineCache,hydrateThumbnailsFromPersistentCache,+ readMediaMetadataFromPersistentCache,+ writeMediaMetadataToPersistentCache,writeThumbnailToPersistentCache,} from './persistent-cache/persistent-cache';export {diff --git a/packages/timeline-utils/src/persistent-cache/persistent-cache.ts b/packages/timeline-utils/src/persistent-cache/persistent-cache.tsindex 889cf411c9..e1224c9333 100644--- a/packages/timeline-utils/src/persistent-cache/persistent-cache.ts+++ b/packages/timeline-utils/src/persistent-cache/persistent-cache.ts@@ -32,6 +32,8 @@ type SourceRecord = {readonly lastAccessedAt: number;readonly aspectRatio: number | null;readonly durationInSeconds: number | null;+ // Owned by the caller, for example the Studio's media metadata.+ readonly mediaMetadata: unknown;readonly waveformBytes: number;readonly thumbnailBytes: number;};@@ -417,6 +419,7 @@ const getWritableDatabase = (src: string): Promise<IDBDatabase | null> => {lastAccessedAt: now,aspectRatio: null,durationInSeconds: null,+ mediaMetadata: null,waveformBytes: 0,thumbnailBytes: 0,} satisfies SourceRecord);@@ -436,6 +439,52 @@ const onWriteError = (error: unknown) => {}};+// Lets the timeline draw a media clip before its metadata is fetched again.+export const readMediaMetadataFromPersistentCache = async (+ src: string,+): Promise<unknown> => {+ try {+ const database = await getReadableDatabase(src);+ if (!database) {+ return null;+ }++ const source: SourceRecord | undefined = await toPromise(+ database.transaction('sources').objectStore('sources').get(src),+ );+ return source?.mediaMetadata ?? null;+ } catch {+ return null;+ }+};++export const writeMediaMetadataToPersistentCache = ({+ src,+ mediaMetadata,+}: {+ readonly src: string;+ readonly mediaMetadata: unknown;+}) => {+ (async () => {+ const database = await getWritableDatabase(src);+ if (!database) {+ return;+ }++ const transaction = database.transaction('sources', 'readwrite');+ const sources = transaction.objectStore('sources');+ const source: SourceRecord | undefined = await toPromise(sources.get(src));+ // The source was evicted or the cache was cleared in the meantime.+ if (!source) {+ transaction.abort();+ return;+ }++ sources.put({...source, mediaMetadata} satisfies SourceRecord);+ await transactionDone(transaction);+ })().catch(onWriteError);+};+export const readWaveformFromPersistentCache = async ({src,waveformSampleRate,