🎥 Make videos programmatically with React
reserving the right amount of space
Jonny Burger2e074a7d53dc
diff --git a/packages/web-renderer/src/mediabunny-mappings.ts b/packages/web-renderer/src/mediabunny-mappings.tsindex f4600b6744..2bfbb272d0 100644--- a/packages/web-renderer/src/mediabunny-mappings.ts+++ b/packages/web-renderer/src/mediabunny-mappings.ts@@ -75,7 +75,7 @@ export const containerToMediabunnyContainer = (): OutputFormat => {switch (container) {case 'mp4':- return new Mp4OutputFormat({fastStart: 'in-memory'});+ return new Mp4OutputFormat({fastStart: 'reserve'});case 'webm':return new WebMOutputFormat();case 'mkv':@@ -89,7 +89,7 @@ export const containerToMediabunnyContainer = (case 'ogg':return new OggOutputFormat();case 'mov':- return new MovOutputFormat({fastStart: 'in-memory'});+ return new MovOutputFormat({fastStart: 'reserve'});default:throw new Error(`Unsupported container: ${container satisfies never}`);}diff --git a/packages/web-renderer/src/render-media-on-web.tsx b/packages/web-renderer/src/render-media-on-web.tsxindex 4ddaab0e8a..bcf14b349c 100644--- a/packages/web-renderer/src/render-media-on-web.tsx+++ b/packages/web-renderer/src/render-media-on-web.tsx@@ -333,9 +333,17 @@ const internalRenderMediaOnWeb = async <}): null;+ const totalFrames = realFrameRange[1] - realFrameRange[0] + 1;+ const durationInSeconds = totalFrames / resolved.fps;+if (videoSampleSource) {outputWithCleanup.output.addVideoTrack(videoSampleSource.videoSampleSource,+ {+ // 1 packet per frame, + 33% buffer+ // https://mediabunny.dev/api/BaseTrackMetadata#maximumpacketcount+ maximumPacketCount: Math.ceil(totalFrames * 1.33),+ },);}@@ -350,6 +358,11 @@ const internalRenderMediaOnWeb = async <if (audioSampleSource) {outputWithCleanup.output.addAudioTrack(audioSampleSource.audioSampleSource,+ {+ // ~1 packet per 10ms, + 33% buffer+ // https://mediabunny.dev/api/BaseTrackMetadata#maximumpacketcount+ maximumPacketCount: Math.ceil(durationInSeconds * 100 * 1.33),+ },);}