npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

v4l2-camera-ts

v1.0.1

Published

Camera library for Linux (V4L2)

Downloads

15

Readme

v4l2-camera-ts

Camera library for Linux (V4L2) written in pure TypeScript using libv4l2-ts for the interaction with the kernel.

Installation

npm install v4l2-camera-ts

Usage

import { Camera } from "v4l2-camera-ts";
import fsp from "fs/promises";

async function main() {
	const cam = new Camera();

	cam.open("/dev/video0");

	console.log(cam.queryFormat());

	// the format can only be set before starting
	cam.setFormat({ width: 1920, height: 1080, pixelFormatStr: "MJPG" });
	
	// allocate memory-mapped buffers and turn the stream on
	cam.start();

	// capture 10 frames and write them to JPEG files
	for (let i = 0; i < 10; i++) {
		// asynchronously wait until the camera fd is readable
		// and then exchange one of the memory-mapped buffers
		const frame = await cam.getNextFrame();

		await fsp.writeFile(`./test/frame-${i}.jpg`, frame);
	}

	// turn the stream off and unmap all buffers
	cam.stop();

	cam.close();
}

main();

Methods & Classes

Camera

open(path: string): void
Opens the camera at the specified path.

close(): void
Closes the camera. This needs to be the last operation done on the camera, otherwise its going to throw an error about the device being busy.

queryFormat(): GetCameraFormat
Fetches information about the currently set format from the camera. You can use this to determine if your setFormat call was successful.

setFormat(format: SetCameraFormat): void
Set the width, height, pixel format and optionally the fps of the camera. The fps can only be set for some pixel formats however (not for MJPG for example).

queryControls(): CameraControl[]
Returns a list of all controls on the camera as well as all menu entries for menu controls.

getControl(id: number): number
Get the current value for a control. Valid numbers for the id parameter can be found in v4l2_controls in libv4l2-ts. The constants always begin with V4L2_CID_.

setControl(id: number, value: number): number
Sets a control value. Valid numbers for the id parameter can be found in v4l2_controls in libv4l2-ts. The constants always begin with V4L2_CID_. The function returns the value that was provided in the value parameter.

start(bufferCount: number = 2): void
Create and memory map buffers which are used for data exchange and turn the video stream on. After this call returns, video frames can be retrieved with getNextFrame().

stop(): void
Stops the stream and unmaps all buffers. After this call, buffers returned from getNextFrame() should not be accessed anymore, so make sure to copy all the data out of them before stopping the stream.

async getNextFrame(): Promise<Buffer>
Wait for the next frame to be available, exchange a buffer with the driver and then return that buffer. The buffer will contain data in the format set by setFormat. The returned buffers are not normal Buffers however, since their underlying data points to a memory-mapped region that the V4L2 driver can write into. This means that you need to copy the data out of them (e.g. with Buffer.from(buf)), or they are going to get overwritten by a later call to getNextFrame().