Skip to content

Commit 1c87ad1

Browse files
committed
FIX: fps
1 parent 17ed598 commit 1c87ad1

File tree

6 files changed

+83
-16
lines changed

6 files changed

+83
-16
lines changed

dist/script/index.js

+38-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/script/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/script/fps-ticker.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export class FPSTicker {
2+
private readonly interval: number;
3+
private lastTime: number = 0;
4+
private onTick: (deltaTime: number) => void = () => {};
5+
private timeoutId: NodeJS.Timeout | null = null;
6+
7+
constructor(fps: number) {
8+
this.interval = 1000 / fps;
9+
}
10+
11+
public start(onTick: (deltaTime: number) => void = () => {}) {
12+
this.onTick = onTick;
13+
this.lastTime = Date.now();
14+
this.tick();
15+
}
16+
17+
public stop() {
18+
clearTimeout(this.timeoutId!);
19+
}
20+
21+
private tick() {
22+
const now = Date.now();
23+
const deltaTime = now - this.lastTime;
24+
25+
if (deltaTime >= this.interval) {
26+
this.lastTime = now - (deltaTime % this.interval); // Adjust for drift
27+
this.onTick(deltaTime);
28+
}
29+
30+
this.timeoutId = setTimeout(() => this.tick(), this.interval - (Date.now() - this.lastTime));
31+
}
32+
}

src/script/real-time-renderer.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import {PNG} from 'pngjs';
33
import {Args} from './cli';
44
import {StatsPrinter} from './stats-printer';
55
import {AbstractRenderer} from './abstract-renderer';
6+
import {FPSTicker} from './fps-ticker';
67

78
export class RealTimeRenderer extends AbstractRenderer {
89
private inputStream: PassThrough | null = null;
9-
private intervalId: NodeJS.Timeout | null = null;
1010
private lastFrame: Buffer;
11+
private readonly ticker: FPSTicker;
1112

1213
constructor(args: Args) {
1314
super(args);
@@ -17,6 +18,7 @@ export class RealTimeRenderer extends AbstractRenderer {
1718
colorType: 6,
1819
});
1920
this.lastFrame = PNG.sync.write(empty);
21+
this.ticker = new FPSTicker(args.fps);
2022
}
2123

2224
public startEncoding() {
@@ -31,6 +33,9 @@ export class RealTimeRenderer extends AbstractRenderer {
3133
`-s ${this.args.videoWidth}x${this.args.videoHeight}`, // Frame size
3234
`-r ${this.args.fps}`, // Framerate
3335
])
36+
.outputOptions([
37+
`-vf fps=fps=${this.args.fps}`, // Framerate
38+
])
3439
.on('start', () => {
3540
console.log('FFmpeg process started.');
3641
})
@@ -47,18 +52,17 @@ export class RealTimeRenderer extends AbstractRenderer {
4752
command.run();
4853

4954
// Produce frames in required rate
50-
const intervalDuration = Math.round(1000 / 30);
51-
this.intervalId = setInterval(() => {
55+
this.ticker.start(() => {
5256
this.inputStream!.write(this.lastFrame);
53-
}, intervalDuration);
57+
});
5458
}
5559

5660
public addFrame(frame: Buffer) {
5761
this.lastFrame = frame;
5862
}
5963

6064
public endEncoding() {
61-
clearTimeout(this.intervalId!);
65+
this.ticker.stop();
6266
this.inputStream!.end();
6367
}
6468
}

src/script/step-recorder.ts

-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ export class StepRecorder extends AbstractRecorder {
4545
this.progressBar.increment();
4646
}
4747

48-
// Finish with en empty frame
49-
this.renderer.addEmptyFrame();
50-
5148
this.progressBar.stop();
5249
await this.renderer.endEncoding();
5350
} catch (error) {

src/script/step-renderer.ts

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ export class StepRenderer extends AbstractRenderer {
6161
'-f concat', // concat frames from the frame list
6262
'-safe 0' // to prevent errors related to unsafe filenames
6363
])
64+
.outputOptions([
65+
`-vf fps=fps=${this.args.fps}`, // Framerate
66+
])
6467
.on('progress', (progress: Object) => {
6568
statsPrinter.print(progress);
6669
})

0 commit comments

Comments
 (0)