diff --git a/README.md b/README.md index c598f2b..0f80314 100644 --- a/README.md +++ b/README.md @@ -129,4 +129,4 @@ Yes.

I get errors related to FFMPEG, why?

Please ensure that you have FFMPEG installed on your PC

run from your cmd / terminal (in linux) the command: ffmpeg --help

-

If you see results, and don't see "command not found" errors, this plugin should work with your FFMPEG

\ No newline at end of file +

If you see results, and don't see "command not found" errors, this plugin should work with your FFMPEG

diff --git a/handlers/FsHandler.js b/handlers/FsHandler.js index 9bb62fb..7aa41cb 100644 --- a/handlers/FsHandler.js +++ b/handlers/FsHandler.js @@ -1,11 +1,15 @@ -const { appendFile, mkdir } = require('fs').promises; +const { appendFile, mkdir, rmdir } = require('fs').promises; const { openSync, closeSync, existsSync } = require('fs'); const { join } = require('path'); class FsHandler { - async init(outputFolder) { + async init(outputFolder, name = Date.now()) { this.outputFolder = outputFolder; - this.videoFilename = join(this.outputFolder, Date.now() + '.webm'); + + // do not put the video in the test subfolder, but in the root recording folder + const videofilename = `../${name}.webm`; + + this.videoFilename = join(this.outputFolder, videofilename); this.imagesPath = join(this.outputFolder, 'images'); this.imagesFilename = join(this.outputFolder, 'images.txt'); await this.verifyPathExists(this.outputFolder); @@ -13,6 +17,12 @@ class FsHandler { await this.verifyPathExists(this.imagesFilename, 'file'); } + async clearOutputSubFolder() { + if (await existsSync(this.outputFolder)) { + await rmdir(this.outputFolder, { recursive: true }); + } + } + createEmptyFile(filename) { return closeSync(openSync(filename, 'w')); } diff --git a/index.js b/index.js index 11c4ebb..5a7c684 100644 --- a/index.js +++ b/index.js @@ -8,23 +8,45 @@ class PuppeteerVideoRecorder { this.fsHandler = new FsHandler(); } - async init(page, outputFolder){ + async init(page, outputFolder, name = Date.now()){ this.page = page; this.outputFolder = outputFolder; - await this.fsHandler.init(outputFolder); - const { imagesPath,imagesFilename, appendToFile } = this.fsHandler; + this.testName = name; + + // append sub-folder name to output folder to form the full output folder name + this.fullOutputFolder = `${this.outputFolder.replace(/\/$/, "")}/${this.testName.replace(/ /gi, '_').toLowerCase()}/`; + + await this.fsHandler.init(this.fullOutputFolder, this.testName); + + const { imagesPath, imagesFilename, appendToFile } = this.fsHandler; + await this.screenshots.init(page, imagesPath, { - afterWritingImageFile: (filename) => appendToFile(imagesFilename, `file '${filename}'\n`) + // strip the full output foldername from the filename to prevent FFMPEG throwing errors + afterWritingImageFile: (filename) => { + appendToFile(imagesFilename, `file 'images${filename.split(imagesPath)[1]}'\n`) + } }); - } + } + // start recording images start(options = {}) { return this.screenshots.start(options); } - + + // stop recording and save images as videofile async stop () { await this.screenshots.stop(); - return this.createVideo(); + await this.createVideo(); + } + + // clear the subfolder with images + async clear () { + await this.fsHandler.clearOutputSubFolder(); + } + + // stop recording, do not save a video + async cancel () { + await this.screenshots.stop(); } get defaultFFMpegCommand() { @@ -35,17 +57,20 @@ class PuppeteerVideoRecorder { '-safe 0', `-i ${imagesFilename}`, '-framerate 60', - videoFilename + `"${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); - }); + + return new Promise((resolve, reject) => { + exec(_ffmpegCommand, (error) => { + if (error) reject(error); + + resolve(); + }); + }); } }