使用puppeteer-recorder
录制来自浏览器活动的视频
此包裹 https://www.npmjs.com/package/puppeteer-recorder
这是我的代码
async function check_login()
{
try {
const page = await global_browser.newPage();
await page.setViewport({width: 1000, height: 1100});
record({
browser: global_browser, // Optional: a puppeteer Browser instance,
page: page, // Optional: a puppeteer Page instance,
output: path + 'output.webm',
fps: 60,
frames: 60 * 5, // 5 seconds at 60 fps
prepare: function () {}, // <-- add this line
render: function () {} // <-- add this line
});
await page.goto('http://localhost/home_robot/mock.php', {timeout: 60000})
.catch(function (error) {
throw new Error('TimeoutBrows');
});
await page.close();
}
catch (e) {
console.log(' LOGIN ERROR ---------------------');
console.log(e);
}
}
它工作正常,但我不知道如何停止记录,因此当我到达功能末尾时,会出现此错误
(node:10000) UnhandledPromiseRejectionWarning: Error: Protocol error (Emulation.setDefaultBackgroundColorOverride): Target closed.
at Promise (C:\wamp64\www\home_robot\node_modules\puppeteer\lib\Connection.js:202:56)
at new Promise (<anonymous>)
at CDPSession.send (C:\wamp64\www\home_robot\node_modules\puppeteer\lib\Connection.js:201:12)
at Page._screenshotTask (C:\wamp64\www\home_robot\node_modules\puppeteer\lib\Page.js:806:26)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:10000) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (
rejection id: 1)
(node:10000) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我没有得到视频文件,我认为是因为我关闭页面而不停止记录
不幸的是,没有文档,并且作者没有回答问题
答案 0 :(得分:1)
无法或无需停止使用此模块进行记录,请仔细查看frames
方法的record
选项:
frames: 60 * 5, // 5 seconds at 60 fps
获取300帧后,它将自行停止。
无论如何,它必须满足一个非常重要的要求,并且文档中不会提及它,但是您可以在模块的源代码中看到它:它使用ffmpeg
从屏幕截图中制作视频,并且必须处于PATH中。如果没有,您应该在选项中提供ffmpeg二进制位置:
const puppeteer = require('puppeteer');
const { record } = require('puppeteer-recorder');
puppeteer.launch({headless : false}).then(async browser => {
const page = await browser.newPage();
await page.goto('https://codepen.io/hexagoncircle/full/joqYEj', {waitUntil : 'networkidle2'});
await record({
ffmpeg: "c:\\ffmpeg\\bin\\ffmpeg.exe" // <-- provide full path to ffmpeg binary
browser: browser, // Optional: a puppeteer Browser instance,
page: page, // Optional: a puppeteer Page instance,
output: 'output.webm',
fps: 24,
frames: 24, // desired seconds of recording multiplied by fps
prepare: function (browser, page) { /* you could click on a button */ },
render: function (browser, page, frame) { /* executed before each capture */ }
});
await browser.close();
});
结果: