Skip to content

Commit db2d154

Browse files
author
prasana kannan
committed
[New Feature]:: Aspect ratio and videoframeSize feature added
1 parent 11a4ca0 commit db2d154

5 files changed

+58
-32
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ const Config = {
7575
followNewTab: true,
7676
fps: 25,
7777
ffmpeg_Path: '<path of ffmpeg_path>' || null,
78+
videoFrame: {
79+
width: 1024,
80+
height: 768,
81+
},
82+
aspectRatio: '4:3',
7883
};
7984
```
8085

@@ -84,6 +89,11 @@ const Config = {
8489
8590
> - **ffmpeg_Path**: String value pointing to the installation of [FFMPEG](https://ffmpeg.org/). Default is null (Automatically install the FFMPEG and use it).
8691
92+
> - **videoFrame**: An object which is to specify the width and height of the capturing video frame. Default to browser viewport size.
93+
94+
> - **aspectRatio**: Specify the apsect ratio of the video. Default value is `4:3`.
95+
96+
8797
**3. create a new instance of video recording**
8898

8999
```javascript

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"doc:json": "typedoc src/ --exclude **/*.spec.ts --target ES6 --mode file --json build/docs/typedoc.json",
6969
"doc:publish": "gh-pages -m \"[ci skip] Updates\" -d build/docs",
7070
"version": "standard-version",
71-
"reset-hard": "git clean -dfx && git reset --hard && npm i",
71+
"reset-hard": "git clean -dfx && npm i",
7272
"prepare-release": "run-s reset-hard test doc:html version doc:publish",
7373
"ci": "run-s reset-hard doc:html version doc:publish"
7474
},

src/lib/PuppeteerScreenRecorder.ts

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ const defaultPuppeteerScreenRecorderOptions: PuppeteerScreenRecorderOptions = {
1717
followNewTab: true,
1818
fps: 25,
1919
ffmpeg_Path: null,
20+
videoFrame: {
21+
width: null,
22+
height: null,
23+
},
24+
aspectRatio: '4:3',
2025
};
2126

2227
/**

src/lib/pageVideoStreamWriter.ts

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import { pageScreenFrame } from './pageVideoStreamCollector';
88
export type VideoOptions = {
99
readonly fps?: number;
1010
readonly ffmpeg_Path?: string | null;
11+
readonly videoFrame?: {
12+
width: number | null;
13+
height: number | null;
14+
};
15+
readonly aspectRatio?: '3:2' | '4:3' | '16:9';
1116
};
1217

1318
/**
@@ -45,6 +50,12 @@ export default class PageVideoStreamWriter extends EventEmitter {
4550
this.configureVideoFile(savePath);
4651
}
4752

53+
private get videoFrameSize(): string {
54+
const { width, height } = this.options.videoFrame;
55+
56+
return width !== null && height !== null ? `${width}x${height}` : '100%';
57+
}
58+
4859
private getFfmpegPath(): string | null {
4960
if (this.options.ffmpeg_Path) {
5061
return this.options.ffmpeg_Path;
@@ -79,6 +90,8 @@ export default class PageVideoStreamWriter extends EventEmitter {
7990
this.writerPromise = new Promise((resolve) => {
8091
ffmpeg({ source: this.videoMediatorStream, priority: 20 })
8192
.videoCodec('libx264')
93+
.size(this.videoFrameSize)
94+
.aspect(this.options.aspectRatio || '4:3')
8295
.inputFormat('image2pipe')
8396
.inputFPS(this.options.fps)
8497
.outputOptions('-preset ultrafast')

src/spec/PuppeteerScreenRecorder.spec.ts

+29-31
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import fs from 'fs';
33
import test from 'ava';
44
import puppeteer from 'puppeteer';
55

6-
import { PuppeteerScreenRecorder } from '../lib/PuppeteerScreenRecorder';
6+
import {
7+
PuppeteerScreenRecorder,
8+
PuppeteerScreenRecorderOptions,
9+
} from '../lib/PuppeteerScreenRecorder';
710

811
test('case 1 --> Happy Path: Should be able to create a new screen-recording session', async (assert) => {
912
/** setup */
@@ -74,37 +77,32 @@ test('test 3 -> Error Path: should throw error if an invalid savePath argument i
7477
}
7578
});
7679

77-
// test('case 3 --> Happy Path: Should be follow new tab open and record it successfully', async (assert) => {
78-
// /** setup */
79-
// const browser = await puppeteer.launch({ headless: true });
80-
// const page = await browser.newPage();
81-
82-
// const outputVideoPath = './test-output/test/video-recorder/testCase3.mp4';
83-
// const recorder = new PuppeteerScreenRecorder(page);
84-
// await recorder.start(outputVideoPath);
85-
86-
// /** execute */
87-
// await page.goto('https://prasanaworld.herokuapp.com', { waitUntil: 'load' });
88-
89-
// await page.waitForSelector('.openLog');
90-
// const link = await page.$('.openLog');
91-
// const newPagePromise = new Promise((resolve) =>
92-
// browser.once('targetcreated', (target) => resolve(target.page()))
93-
// ); // declare promise
94-
// await link.click({ button: 'middle' });
95-
// const page2: Page = await newPagePromise;
96-
97-
// await page2.bringToFront();
80+
test('case 2 --> Happy Path: testing video recording with video frame width, height and aspect ratio', async (assert) => {
81+
/** setup */
82+
const browser = await puppeteer.launch({ headless: true });
83+
const page = await browser.newPage();
9884

99-
// await page2.close();
85+
const options: PuppeteerScreenRecorderOptions = {
86+
followNewTab: false,
87+
videoFrame: {
88+
width: 1024,
89+
height: 1024,
90+
},
91+
aspectRatio: '4:3',
92+
};
93+
const outputVideoPath = './test-output/test/video-recorder/testCase4.mp4';
94+
const recorder = new PuppeteerScreenRecorder(page, options);
95+
const recorderValue = await recorder.start(outputVideoPath);
10096

101-
// await page.goto('https://google.com', { waitUntil: 'load' });
97+
/** execute */
98+
await page.goto('https://github.com', { waitUntil: 'load' });
99+
/** clear */
100+
const status = await recorder.stop();
102101

103-
// /** clear */
104-
// const status = await recorder.stop();
105-
// await browser.close();
102+
await browser.close();
106103

107-
// /** assert */
108-
// assert.is(status, true);
109-
// assert.is(fs.existsSync(outputVideoPath), true);
110-
// });
104+
/** assert */
105+
assert.is(recorderValue instanceof PuppeteerScreenRecorder, true);
106+
assert.is(status, true);
107+
assert.is(fs.existsSync(outputVideoPath), true);
108+
});

0 commit comments

Comments
 (0)