Repo of the Day
launchcodedev/pi-camera-connect
Published: Nov 18, 2023
Open repository ↗Library to capture and stream Raspberry Pi camera data directly to NodeJS
Summary
pi-camera-connect is a TypeScript library that captures stills and video from a Raspberry Pi camera directly into Node.js without going through the filesystem. It exposes two main classes, StillCamera and StreamCamera, and uses the Pi's hardware-accelerated H264 and MJPEG encoders so that frames arrive as Buffer objects or standard stream.Readable streams.
What it is useful for
This package fits a narrow but practical niche: Node.js projects running on a Raspberry Pi that need to grab camera frames programmatically. The StillCamera class wraps the equivalent of raspistill and returns a JPEG Buffer, making it straightforward to save an image, push it to an HTTP response, or hand it off to an image-processing pipeline. The StreamCamera class supports two codecs. H264 is intended for video capture, while MJPEG is used for very fast single-frame grabs at roughly the stream's frame rate (the README cites ~33 ms at 30 fps, with the trade-off of grainier, less noise-reduced images than the still port).
Because the data stays in memory as a Buffer or Readable stream, you can pipe it to a file, an HTTP response, or a WebSocket without intermediate disk I/O. The library ships with TypeScript definitions, making it usable in strongly-typed codebases.
A few documented constraints to keep in mind:
- Output from
H264captures is a raw H264 elementary stream, not an MP4 or MKV container. The README notes that wrapping it in a container is out of scope, and suggestsomxplayerfor playback on Raspbian. StreamCamera.takeImage()only works whencodecis set toCodec.MJPEG; otherwise it throws.- Readable streams buffer in memory if you don't consume them, so an un-piped stream will exhaust RAM quickly. The README recommends using
pipe(),resume(), or a'data'listener. - Available
SensorModevalues and their resolution/fps ranges differ between camera v1 (OV5647) and v2 (IMX219), and the README provides tables for each.
How engineers can use it
Install the package from npm:
$ npm install --save pi-camera-connect
or with Yarn:
$ yarn add pi-camera-connect
Then import the classes. A still capture looks like:
import { StillCamera } from 'pi-camera-connect';
import * as fs from 'fs';
const stillCamera = new StillCamera();
const image = await stillCamera.takeImage();
fs.writeFileSync('still-image.jpg', image);
For a five-second H264 video, you create a StreamCamera with Codec.H264, pipe its readable stream to a file, then call startCapture() and stopCapture(). A fast JPEG grab is similar but uses Codec.MJPEG followed by await streamCamera.takeImage().
Because the library talks to the Pi's camera stack, it must run on a Raspberry Pi with a supported camera module attached; behavior on non-Pi hardware is not documented. For the full option list (rotation, flip, exposure, AWB, sensor mode, etc.), see the README.