2

I'm trying to record video and audio using puppeteer stream, but it's getting written only the video to the output file:

async function videoRecorder() {
  require("puppeteer-stream");
  const puppeteer = require("puppeteer");
  const fs = require("fs");

  const filename = `./recordings/test.mp4`;

  const file = fs.createWriteStream(filename);

  const browser = await puppeteer.launch({
    executablePath:
      "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
    headless: true,
    defaultViewport: null,
    devtools: false,
    args: [
      "--window-size=1920,1080",
      "--window-position=1921,0",
      "--autoplay-policy=no-user-gesture-required",
    ],
    ignoreDefaultArgs: ["--mute-audio"],
  });

  const page = await browser.newPage();

  await page.goto(pathToPageWithVideo, {
    waitUntil: "load",
  });

  const stream = await page.getStream({
    audio: true,
    video: true,
  });

  stream.pipe(file);

  setTimeout(async () => {
    await stream.destroy();
    file.close();
    console.log("finished");
  }, 10000);
}

I've tried many other ways of recording video and audio but this is the one I got closer, since my main purpose is to get the audio only, I'm open to more suggestions.

Thanks

dazzed
  • 643
  • 2
  • 9
  • 19
  • I'm working on a similar problem statement. Is it possible to share a gist of what you did here? I'm stuck in the part where you're calling page.getStream. page is imported from puppeteer and not puppeteer-stream and hence gives me an error: 'TypeError: page.getStream is not a function'. I'm trying to build on top of your script because I need to use headless mode as well. – Shreyas Jun 24 '23 at 06:05

1 Answers1

2

Solved by saving as .webm instead of .mp4.

Reference: https://github.com/Flam3rboy/puppeteer-stream/issues/1

dazzed
  • 643
  • 2
  • 9
  • 19