forked from shaynet10/puppeteer-video-recorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (45 loc) · 1.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const { FsHandler } = require('./handlers');
const { exec } = require('child_process');
const PuppeteerMassScreenshots = require('puppeteer-mass-screenshots');
class PuppeteerVideoRecorder {
constructor(){
this.screenshots = new PuppeteerMassScreenshots();
this.fsHandler = new FsHandler();
}
async init(page, outputFolder){
this.page = page;
this.outputFolder = outputFolder;
await this.fsHandler.init(outputFolder);
const { imagesPath,imagesFilename, appendToFile } = this.fsHandler;
await this.screenshots.init(page, imagesPath, {
afterWritingImageFile: (filename) => appendToFile(imagesFilename, `file '${filename}'\n`)
});
}
start(options = {}) {
return this.screenshots.start(options);
}
async stop () {
await this.screenshots.stop();
return this.createVideo();
}
get defaultFFMpegCommand() {
const { imagesFilename, videoFilename } = this.fsHandler;
return [
'ffmpeg',
'-f concat',
'-safe 0',
`-i ${imagesFilename}`,
'-framerate 60',
videoFilename
].join(' ');
}
createVideo(ffmpegCommand = '') {
const _ffmpegCommand = ffmpegCommand || this.defaultFFMpegCommand;
exec(_ffmpegCommand, (error, stdout, stderr) => {
if (error) throw new Error(error);
console.log(stdout);
console.log(stderr);
});
}
}
module.exports = PuppeteerVideoRecorder;