Screen recording with Puppeteer fails on Ubuntu: NotReadableError when using MediaRecorder API

I’m having trouble with screen recording on Ubuntu using Puppeteer and the MediaRecorder API. It works fine on Windows but not on Ubuntu. Here’s what I’m trying to do:

async function captureScreen() {
  const displayStream = await navigator.mediaDevices.getDisplayMedia({
    video: { width: 1920, height: 1080 },
  });

  const recorder = new MediaRecorder(displayStream);

  recorder.ondata = (e) => {
    // Handle video data
  };
}

When I run this on Ubuntu, I get a NotReadableError when trying to get the display media. I’ve tried a bunch of things:

  • Running the browser with --use-fake-ui-for-media-stream
  • Setting --auto-select-desktop-capture-source=Entire screen
  • Turning on AudioServiceOutOfProcess
  • Turning off sandbox stuff

None of these fixed it. The browser is running in non-headless mode. I just want to record the whole screen on Ubuntu like I can on Windows. Any ideas what might be causing this or how to fix it?

I’ve encountered this problem before when attempting screen recording on Ubuntu. In my experience, using x11vnc in combination with a virtual X server has helped resolve the NotReadableError. I started a virtual display using Xvfb and then launched x11vnc targeting that display. This setup allowed Puppeteer to connect to the virtual display properly. Additionally, launching the browser with flags such as --no-sandbox, --disable-setuid-sandbox, and --use-fake-ui-for-media-stream ensured the environment was compatible with the MediaRecorder API. This approach helped me bypass the recording error.

hey jackhero, i had similar issues on ubuntu. try using xvfb-run to create a virtual display. something like:

xvfb-run --server-args=‘-screen 0 1920x1080x24’ your_puppeteer_script.js

this worked for me when i couldnt get screen capture working normally. good luck!

I’ve dealt with this exact issue on Ubuntu before. What finally worked for me was using FFmpeg instead of the MediaRecorder API. It’s a bit more involved, but way more reliable on Linux systems. Here’s the gist:

  1. Install FFmpeg on your Ubuntu machine
  2. In your Puppeteer script, launch a new page and navigate to the desired URL
  3. Get the page dimensions
  4. Use child_process.spawn to start FFmpeg, capturing the screen
  5. Let it record for the desired duration
  6. Stop the FFmpeg process

The FFmpeg command I used was something like:

ffmpeg -f x11grab -s 1920x1080 -i :0.0 output.mp4

This bypasses the whole MediaRecorder headache and gives you a lot more control over the output. It took some trial and error, but it’s been rock-solid for me ever since. Hope this helps!