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

Commit

Permalink
Merge pull request #33 from qawolf/fix-frame-order
Browse files Browse the repository at this point in the history
fix: frame order 🤦
  • Loading branch information
jperl authored May 29, 2020
2 parents c30ae85 + a8ade61 commit 741411c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/PageVideoCapture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class PageVideoCapture {
// write the previous frame based on the duration between it and the current frame
if (this._previousFrame) {
const durationSeconds =
(currentFrame.received - this._previousFrame.received) / 1000;
currentFrame.timestamp - this._previousFrame.timestamp;
this._writer.write(this._previousFrame.data, durationSeconds);
}

Expand All @@ -88,7 +88,8 @@ export class PageVideoCapture {

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

Expand Down
5 changes: 4 additions & 1 deletion src/VideoWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ export class VideoWriter extends EventEmitter {
public write(data: Buffer, durationSeconds = 1): void {
this._receivedFrame = true;

const numFrames = Math.round(durationSeconds * this._framesPerSecond);
const numFrames = Math.max(
Math.round(durationSeconds * this._framesPerSecond),
1,
);
debug(`write ${numFrames} frames for duration ${durationSeconds}s`);

for (let i = 0; i < numFrames; i++) {
Expand Down
7 changes: 6 additions & 1 deletion tests/saveVideo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ describe('saveVideo', () => {
const savePath = join(tmpdir(), `${Date.now()}.mp4`);

const capture = await saveVideo(page, savePath);
await page.setContent('<html>hello world</html>');

for (let i = 0; i < 10; i++) {
await page.setContent(`<html>hello world ${i}</html>`);
await new Promise((r) => setTimeout(r, 100));
}

await capture.stop();

const videoPathExists = await pathExists(savePath);
Expand Down

0 comments on commit 741411c

Please sign in to comment.