@@ -101,7 +101,7 @@ class FfmpegVideoRecorder {
101101 private _process : ChildProcess | null = null ;
102102 private _gracefullyClose : ( ( ) => Promise < void > ) | null = null ;
103103 private _firstFrameTimestamp : number = 0 ;
104- private _lastFrame : { timestamp : number , frameNumber : number , timestampMs : number , buffer : Buffer } | null = null ;
104+ private _lastFrame : { timestamp : number , frameNumber : number , buffer : Buffer } | null = null ;
105105 private _lastWriteNodeTime : number = 0 ;
106106 private _isStopped = false ;
107107 private _ffmpegPath : string ;
@@ -206,23 +206,17 @@ class FfmpegVideoRecorder {
206206
207207 if ( ! this . _firstFrameTimestamp )
208208 this . _firstFrameTimestamp = timestamp ;
209- const timestampMs = Math . max ( 0 , Math . round ( ( timestamp - this . _firstFrameTimestamp ) * 1000 ) ) ;
210209
211- // The output is constant frame rate, so multiple input frames that map to the same output
212- // slot would be redundant - ffmpeg only keeps one of them. We coalesce them on our side to
213- // avoid muxing and piping frames that ffmpeg would just discard, keeping the most recent
214- // frame for each slot. The pending frame is only emitted once the slot is complete (a frame
215- // belonging to a later slot arrives, or recording stops), so that fast bursts of frames
216- // within a single slot still surface the latest pixels rather than the first ones.
217- const frameNumber = Math . floor ( timestampMs * fps / 1000 ) ;
210+ const frameNumber = Math . floor ( ( timestamp - this . _firstFrameTimestamp ) * fps ) ;
218211 if ( this . _lastFrame && frameNumber !== this . _lastFrame . frameNumber )
219- this . _emitFrame ( this . _lastFrame . buffer , this . _lastFrame . timestampMs ) ;
212+ this . _emitFrame ( this . _lastFrame . buffer , this . _lastFrame . frameNumber ) ;
220213
221- this . _lastFrame = { buffer : frame , timestamp, frameNumber, timestampMs } ;
214+ this . _lastFrame = { buffer : frame , timestamp, frameNumber } ;
222215 this . _lastWriteNodeTime = monotonicTime ( ) ;
223216 }
224217
225- private _emitFrame ( frame : Buffer , timestampMs : number ) {
218+ private _emitFrame ( frame : Buffer , frameNumber : number ) {
219+ const timestampMs = Math . max ( 0 , Math . round ( frameNumber * 1000 / fps ) ) ;
226220 this . _process ! . stdin ! . write ( writeClusterHeader ( timestampMs , frame . length ) ) ;
227221 this . _process ! . stdin ! . write ( frame ) ;
228222 }
@@ -241,9 +235,10 @@ class FfmpegVideoRecorder {
241235 // Emit the last received frame at its own slot, then repeat it at the end so it stays visible
242236 // for at least 1s. This also ensures non-empty videos with 1 frame and gives the output stream
243237 // a final timestamp.
244- this . _emitFrame ( this . _lastFrame ! . buffer , this . _lastFrame ! . timestampMs ) ;
238+ this . _emitFrame ( this . _lastFrame ! . buffer , this . _lastFrame ! . frameNumber ) ;
245239 const addTime = Math . max ( ( monotonicTime ( ) - this . _lastWriteNodeTime ) / 1000 , 1 ) ;
246- this . _emitFrame ( this . _lastFrame ! . buffer , this . _lastFrame ! . timestampMs + Math . round ( addTime * 1000 ) ) ;
240+ const endFrameNumber = Math . floor ( ( this . _lastFrame ! . timestamp + addTime - this . _firstFrameTimestamp ) * fps ) ;
241+ this . _emitFrame ( this . _lastFrame ! . buffer , endFrameNumber ) ;
247242 this . _isStopped = true ;
248243 try {
249244 await this . _gracefullyClose ! ( ) ;
0 commit comments