1

I want to record my electron app's content using Page.startScreencast command of Chrome DevTools Protocol?

Could not find out any exmaple use. How can achieve this?

amaitland
  • 4,073
  • 3
  • 25
  • 63
Alok
  • 7,734
  • 8
  • 55
  • 100

1 Answers1

3

Sorry my answer is based on Puppeteer, I have no experience in electron. But it seems it is possible to use them together. Not sure it will answer your use case though. There might be a better way to access Chrome Devtools Protocol directly with some Electron's APIs.

https://www.npmjs.com/package/puppeteer-in-electron

For a classical Puppeteer / Node solution

import { Browser, Page } from 'puppeteer'

const browser = await puppeteer.launch({
    // your params
  })

const page = await browser.newPage()
const yourWebSite = 'http://www.whatYouWantToCapture.com'
await page.goto(yourWebSite, {
    waitUntil: 'networkidle0', // Ensure page is finished loading
  })

const client = await page.target().createCDPSession()

//Register a callback on every frame rendered by the browser (framerate depends on a lot of factors)
client.on('Page.screencastFrame', async (frameObject) => {

  // Do what you want with frame, ex write to file on disk
  await fs.writeFile(Date.now()+'.jpeg', frameObject.data, 'base64')
  await this.client.send('Page.screencastFrameAck', {
      sessionId: frameObject.sessionId,
  })
)

// When you want to start
client.send('Page.startScreencast',{
  format: 'jpeg',
  quality: 100,
  maxWidth: 1920,
  maxHeight: 1080,
  everyNthFrame: 1,
})

// When you want to stop
client.send('Page.stopScreencast')
Ronan Quillevere
  • 3,699
  • 1
  • 29
  • 44
  • what should I import to acess `page`? – Alok Oct 02 '21 at 13:36
  • My answer is specific to puppeteer. Do not know if there is any specific electron API to access CDP. @AlokSinghMahor hope it helps – Ronan Quillevere Oct 04 '21 at 08:02
  • 1
    @Alok in Electron it's `win.webContents.debugger.attach('1.3')` and `win.webContents.debugger.sendCommand("Page.startScreencast", ...)`. This is an example of using CDP: https://github.com/mr-yt12/reproduce-fetch-network/blob/master/fetch/index.js – teg_brightly Dec 20 '21 at 19:59
  • @RonanQuillevere Is there a way to set fps? I see it captures 10 frames per second which is too low to render a video. – Murat Colyaran Mar 11 '22 at 09:44