@@ -93,19 +93,25 @@ export class VideoRecorder {
93
93
assert ( this . _process ) ;
94
94
if ( ! this . _isRunning ( ) )
95
95
return ;
96
- const duration = this . _lastFrameTimestamp ? Math . max ( 1 , Math . round ( 25 * ( timestamp - this . _lastFrameTimestamp ) ) ) : 1 ;
97
- this . _progress . log ( `writing ${ duration } frame(s)` ) ;
96
+ const repeatCount = this . _lastFrameTimestamp ? Math . max ( 1 , Math . round ( 25 * ( timestamp - this . _lastFrameTimestamp ) ) ) : 1 ;
97
+ this . _progress . log ( `writing ${ repeatCount } frame(s)` ) ;
98
+ this . _lastWritePromise = this . _flushLastFrame ( repeatCount ) . catch ( e => this . _progress . log ( 'Error while writing frame: ' + e ) ) ;
98
99
this . _lastFrameBuffer = frame ;
99
100
this . _lastFrameTimestamp = timestamp ;
100
101
this . _lastWriteTimestamp = Date . now ( ) ;
102
+ }
101
103
104
+ private async _flushLastFrame ( repeatCount : number ) : Promise < void > {
105
+ assert ( this . _process ) ;
106
+ const frame = this . _lastFrameBuffer ;
107
+ if ( ! frame )
108
+ return ;
102
109
const previousWrites = this . _lastWritePromise ;
103
110
let finishedWriting : ( ) => void ;
104
- this . _lastWritePromise = new Promise ( fulfill => finishedWriting = fulfill ) ;
105
- const writePromise = this . _lastWritePromise ;
111
+ const writePromise = new Promise < void > ( fulfill => finishedWriting = fulfill ) ;
106
112
await previousWrites ;
107
- for ( let i = 0 ; i < duration ; i ++ ) {
108
- const callFinish = i === ( duration - 1 ) ;
113
+ for ( let i = 0 ; i < repeatCount ; i ++ ) {
114
+ const callFinish = i === ( repeatCount - 1 ) ;
109
115
this . _process . stdin . write ( frame , ( error : Error | null | undefined ) => {
110
116
if ( error )
111
117
this . _progress . log ( `ffmpeg failed to write: ${ error } ` ) ;
0 commit comments