3636 * the stages can import them without reaching back into the orchestrator.
3737 */
3838
39+ import { statfsSync } from "node:fs" ;
3940import {
4041 type BeforeCaptureHook ,
4142 type CaptureOptions ,
@@ -136,6 +137,42 @@ export function shouldAllowAdaptiveCaptureRetry(
136137 return workerCount > 1 ;
137138}
138139
140+ export function estimateDiskCaptureBytes (
141+ totalFrames : number ,
142+ captureOptions : CaptureOptions ,
143+ ) : number {
144+ const scale = captureOptions . deviceScaleFactor ?? 1 ;
145+ const outputWidth = Math . ceil ( captureOptions . width * scale ) ;
146+ const outputHeight = Math . ceil ( captureOptions . height * scale ) ;
147+ return Math . ceil ( totalFrames ) * outputWidth * outputHeight * 4 ;
148+ }
149+
150+ export function assertDiskCaptureHeadroom (
151+ framesDir : string ,
152+ totalFrames : number ,
153+ captureOptions : CaptureOptions ,
154+ freeDiskBytes : ( path : string ) => number | null = ( path ) => {
155+ try {
156+ const stat = statfsSync ( path ) ;
157+ return stat . bavail * stat . bsize ;
158+ } catch {
159+ return null ;
160+ }
161+ } ,
162+ ) : void {
163+ const freeBytes = freeDiskBytes ( framesDir ) ;
164+ if ( freeBytes === null ) return ;
165+ const estimatedBytes = estimateDiskCaptureBytes ( totalFrames , captureOptions ) ;
166+ if ( estimatedBytes <= freeBytes * 0.9 ) return ;
167+ throw new Error (
168+ `Disk capture may need ~${ ( estimatedBytes / 1e6 ) . toFixed ( 1 ) } MB of temporary frame storage, ` +
169+ `but only ${ ( freeBytes / 1e6 ) . toFixed ( 1 ) } MB is free at ${ framesDir } . ` +
170+ "Re-run with --low-memory-mode to stream frames, raise " +
171+ "PRODUCER_STREAMING_ENCODE_MAX_DURATION_SECONDS if streaming is supported, " +
172+ "or free up disk space." ,
173+ ) ;
174+ }
175+
139176export async function runCaptureStage ( input : CaptureStageInput ) : Promise < CaptureStageResult > {
140177 const {
141178 fileServer,
@@ -194,6 +231,9 @@ export async function runCaptureStage(input: CaptureStageInput): Promise<Capture
194231 }
195232 }
196233
234+ const captureOptions = buildCaptureOptions ( ) ;
235+ assertDiskCaptureHeadroom ( framesDir , totalFrames , captureOptions ) ;
236+
197237 if ( workerCount > 1 ) {
198238 // Parallel capture. When `frameRange` is set (distributed chunk), pass
199239 // `frameRangeStart` so workers land on absolute composition frame indices
@@ -206,7 +246,7 @@ export async function runCaptureStage(input: CaptureStageInput): Promise<Capture
206246 initialWorkerCount : workerCount ,
207247 allowRetry : shouldAllowAdaptiveCaptureRetry ( workerCount , job . config . workers !== undefined ) ,
208248 frameExt : needsAlpha ? "png" : "jpg" ,
209- captureOptions : buildCaptureOptions ( ) ,
249+ captureOptions,
210250 createBeforeCaptureHook : createRenderVideoFrameInjector ,
211251 abortSignal,
212252 frameRangeStart : frameRange ?. startFrame ,
@@ -252,7 +292,7 @@ export async function runCaptureStage(input: CaptureStageInput): Promise<Capture
252292 ( await createCaptureSession (
253293 fileServer . url ,
254294 framesDir ,
255- buildCaptureOptions ( ) ,
295+ captureOptions ,
256296 videoInjector ,
257297 captureCfg ,
258298 ) ) ;
0 commit comments