Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record and process screen capture at high frame rate in NodeJS

Tags:

node.js

As stated in the title, I would like to capture the screen 60 times a second and run it through a nodejs function.

Most libraries on npm only save to disk.

I tried taking screenshots with robotjs but only achieved 11fps.

I tried starting an RTMP server and using OBS but its delayed.

What in the world do I do?

like image 315
Kirk Avatar asked Apr 02 '18 08:04

Kirk


People also ask

How do I record my screen on high FPS?

Head over to the Video Settings > Frame Rate and select the desired framerate. (Note: The 90 or 120Hz screen recording option will only appear if your smartphone supports these frame rates.) After making changes, tap the video icon on the home screen of the app. Happy recording!

Which frame rate is best for screen recording?

A screen recording, sometimes called a screencast or video screen capture, is a digital recording of rendered scenes (ideally 60 frames per second) on a computer screen, often containing audio narration as well as a webcam or facecam video of the narrator.

How do I take a screenshot of a website in node?

goto("https://nytimes.com"); await page. screenshot({ path: "nyt-puppeteer. png" }); await browser. close(); });

How to change the frame rate of a screen capture?

If the performance of your computer is a little poor, you are able to adjust the appropriate frame rate in the Settings tab, where you are allowed to set the frame rate from 1 to 60 flexibly. Besides, this screen capture lets you capture your screen and save it in different quality, including Standard, High, and original options.

What are the advantages of a high FPS screen recorder?

Speaking generally, the higher the FPS is, and the smoother your videos look. If you are a gamer and want to capture your brilliant game clips with high quality for YouTube or other sites, you'd better find a high FPS screen recorder.

How many frames per second does MSS run?

On the other hand, mss runs much faster than any other screen capture APIs. I used mss to apply object detection ( YOLOv2, darkflow ), and it run at over 40 frames per second. If it is used without any object detection, it should run at more fps.

Does MSS run faster than screen capture?

On the other hand, mss runs much faster than any other screen capture APIs. I used mss to apply object detection ( YOLOv2, darkflow ), and it run at over 40 frames per second.


2 Answers

You can use ffmpeg to achieve 60fps. Take a look at ffmpeg docs for more details.

const ffmpeg = require("ffmpeg-static").path;
const { spawn } = require("child_process");

const process = spawn(
    ffmpeg,
  ["-probesize", "10M", "-f", "gdigrab", "-framerate", "60", "-i", "desktop", "-f", "flv", "-"],
  { stdio: "pipe" }
);
const stream = process.stdout;

Here you have the stream of video. Do the processing.

For example, you can write the stream to a Writable stream

const { createWriteStream } = require("fs");

const file = createWriteStream("capture.flv");
stream.pipe(file);

Or you can subscribe to chunks of data and do some processing

stream.on("data", chunk => {
  console.log(chunk);
});

More about Node.JS streams

Note:

This example would only work for Windows users. Mac and Linux users should change ffmpeg options used to spawn the process as described in the ffmpeg docs.

like image 79
Shant Marouti Avatar answered Nov 15 '22 21:11

Shant Marouti


Your best bet might be to create a native process for doing the capture, using platform-specific APIs, then wrap them in NAN (Native Abstraction for Node: https://github.com/nodejs/nan). There's a little learning curve, but not too bad. I've built systems for decoding JPEGs, manipulating them, and encoding in h.264 this way.

Because you're in control of the capture at the native level, you can create whatever interface you like to bring screen grabs into Node. For example, you might want to avoid marshaling images across the native-javascript interface and instead maintain handles to them, and only pull an image when you need to.

Then you can turn it all into a cool npm module and be adored by everyone.

like image 43
Jim B. Avatar answered Nov 15 '22 22:11

Jim B.