🎥 Make videos programmatically with React
`@remotion/studio`: Fix VideoSample resource leak in timeline filmstrip
Jonny Burger2d62ab380066
diff --git a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsxindex d3732e7857..6deeadf2fe 100644--- a/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx+++ b/packages/studio/src/components/Timeline/TimelineVideoInfo.tsx@@ -378,54 +378,72 @@ export const TimelineVideoInfo: React.FC<{},src,onVideoSample: (sample) => {- const frame = sample.toVideoFrame();- const scale = (HEIGHT / frame.displayHeight) * window.devicePixelRatio;-- const transformed = resizeVideoFrame({- frame,- scale,- });-- if (transformed !== frame) {- frame.close();+ let frame: VideoFrame | undefined;+ try {+ frame = sample.toVideoFrame();+ const scale =+ (HEIGHT / frame.displayHeight) * window.devicePixelRatio;++ const transformed = resizeVideoFrame({+ frame,+ scale,+ });++ if (transformed !== frame) {+ frame.close();+ frame = undefined;+ }++ const databaseKey = makeFrameDatabaseKey(+ src,+ transformed.timestamp,+ );++ const existingFrame = frameDatabase.get(databaseKey);+ if (existingFrame) {+ existingFrame.frame.close();+ }++ frameDatabase.set(databaseKey, {+ frame: transformed,+ lastUsed: Date.now(),+ });+ if (aspectRatio.current === null) {+ throw new Error('Aspect ratio is not set');+ }++ ensureSlots({+ filledSlots,+ fromSeconds,+ toSeconds,+ naturalWidth,+ aspectRatio: aspectRatio.current,+ });+ fillFrameWhereItFits({+ ctx,+ filledSlots,+ visualizationWidth: naturalWidth,+ frame: transformed,+ segmentDuration: toSeconds - fromSeconds,+ fromSeconds,+ });+ } catch (e) {+ if (frame) {+ frame.close();+ }++ throw e;+ } finally {+ sample.close();}-- const databaseKey = makeFrameDatabaseKey(src, transformed.timestamp);-- const existingFrame = frameDatabase.get(databaseKey);- if (existingFrame) {- existingFrame.frame.close();- }-- frameDatabase.set(databaseKey, {- frame: transformed,- lastUsed: Date.now(),- });- if (aspectRatio.current === null) {- throw new Error('Aspect ratio is not set');- }-- ensureSlots({- filledSlots,- fromSeconds,- toSeconds,- naturalWidth,- aspectRatio: aspectRatio.current,- });- fillFrameWhereItFits({- ctx,- filledSlots,- visualizationWidth: naturalWidth,- frame: transformed,- segmentDuration: toSeconds - fromSeconds,- fromSeconds,- });-- sample.close();},signal: controller.signal,}).then(() => {+ if (controller.signal.aborted) {+ return;+ }+fillWithCachedFrames({ctx,naturalWidth,diff --git a/packages/studio/src/helpers/extract-frames.ts b/packages/studio/src/helpers/extract-frames.tsindex b2bc6f0b12..aefa3671d5 100644--- a/packages/studio/src/helpers/extract-frames.ts+++ b/packages/studio/src/helpers/extract-frames.ts@@ -70,18 +70,31 @@ export async function extractFrames({}const sink = new VideoSampleSink(videoTrack);+ const sampleIterator = sink.samplesAtTimestamps(timestamps);- for await (const videoSample of sink.samplesAtTimestamps(timestamps)) {- if (signal?.aborted) {- videoSample?.close();- break;- }+ try {+ for await (const videoSample of sampleIterator) {+ if (signal?.aborted) {+ videoSample?.close();+ break;+ }- if (!videoSample) {- continue;- }+ if (!videoSample) {+ continue;+ }- onVideoSample(videoSample);+ onVideoSample(videoSample);+ }+ } finally {+ // When input.dispose() causes the iterator to throw+ // InputDisposedError, for-await does not call .return() on the+ // iterator (it only does so on `break`). Explicitly call it so+ // the iterator can close any internally buffered VideoSamples.+ try {+ await sampleIterator.return?.();+ } catch {+ // Iterator already done or input disposed+ }}} catch (error) {if (error instanceof InputDisposedError) {