A NodeJS transform stream for chunking raw data into constant-size chunks. Useful for consuming raw media streams where chunk size = 1 frame.
import SizeChunker from "@akiroz/size-chunker-stream";
const chunker = new SizeChunker({ chunkSize: 640 * 480 * 3 });
process.stdin.pipe(chunker);
chunker.on("data", (frame) => {
// Do stuff with frame
});
The SizeChunker
outputs Buffer
of constant size, this does not mean the underlaying ArrayBuffer
is also of the same constant size. To create typed views of the data, it may be nessesary to slice the underlaying ArrayBuffer
:
chunker.on("data", ({ buffer, length, byteOffset }: Buffer) => {
const frame = new Int16Array(buffer.slice(byteOffset, byteOffset + length));
// Do stuff with frame
});