Skip to content
This repository was archived by the owner on Jul 2, 2021. It is now read-only.

Commit a8ade61

Browse files
committed
fix frame order
1 parent c30ae85 commit a8ade61

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

src/PageVideoCapture.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class PageVideoCapture {
7676
// write the previous frame based on the duration between it and the current frame
7777
if (this._previousFrame) {
7878
const durationSeconds =
79-
(currentFrame.received - this._previousFrame.received) / 1000;
79+
currentFrame.timestamp - this._previousFrame.timestamp;
8080
this._writer.write(this._previousFrame.data, durationSeconds);
8181
}
8282

@@ -88,7 +88,8 @@ export class PageVideoCapture {
8888

8989
// write the final frame based on the duration between it and now
9090
debug('write final frame');
91-
const durationSeconds = (Date.now() - this._previousFrame.received) / 1000;
91+
const durationSeconds =
92+
(Date.now() - this._previousFrame.timestamp * 1000) / 1000;
9293
this._writer.write(this._previousFrame.data, durationSeconds);
9394
}
9495

src/VideoWriter.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ export class VideoWriter extends EventEmitter {
7272
public write(data: Buffer, durationSeconds = 1): void {
7373
this._receivedFrame = true;
7474

75-
const numFrames = Math.round(durationSeconds * this._framesPerSecond);
75+
const numFrames = Math.max(
76+
Math.round(durationSeconds * this._framesPerSecond),
77+
1,
78+
);
7679
debug(`write ${numFrames} frames for duration ${durationSeconds}s`);
7780

7881
for (let i = 0; i < numFrames; i++) {

tests/saveVideo.test.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ describe('saveVideo', () => {
1919
const savePath = join(tmpdir(), `${Date.now()}.mp4`);
2020

2121
const capture = await saveVideo(page, savePath);
22-
await page.setContent('<html>hello world</html>');
22+
23+
for (let i = 0; i < 10; i++) {
24+
await page.setContent(`<html>hello world ${i}</html>`);
25+
await new Promise((r) => setTimeout(r, 100));
26+
}
27+
2328
await capture.stop();
2429

2530
const videoPathExists = await pathExists(savePath);

0 commit comments

Comments
 (0)