Skip to content

Instantly share code, notes, and snippets.

@nanki
Created October 21, 2020 01:18
Show Gist options
  • Save nanki/fd693294a5bf315e7218e013c28bea6f to your computer and use it in GitHub Desktop.
Save nanki/fd693294a5bf315e7218e013c28bea6f to your computer and use it in GitHub Desktop.
import { promises as fs } from "fs";
import createContext from "gl";
import beamcoder from "beamcoder";
const w = 100, h = 100;
async function main() {
const decoder = beamcoder.decoder({
name: "rawvideo",
pix_fmt: 'rgba',
width: w,
height: h,
});
const encoder = beamcoder.encoder({
name : 'mjpeg',
width : w,
height: h,
pix_fmt: 'yuvj444p',
time_base: [1, 1]
});
const filterer = await beamcoder.filterer({
filterType: 'video',
inputParams: [
{
width: w,
height: h,
pixelFormat: 'uyvy422',
pixelAspect: [1, 1],
timeBase: [1, 1]
}
],
outputParams: [
{
pixelFormat: 'yuvj444p'
}
],
filterSpec: 'format=pix_fmts=yuvj422p'
});
const gl = createContext(w, h, { preserveDrawingBuffer: true });
gl.clearColor(1, 0, 0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
const b = new Uint8Array(w * h * 4);
gl.readPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, b);
const packet = beamcoder.packet({
data: Buffer.from(b),
size: w * h * 4
});
const decoded = await decoder.decode(packet);
const frame = decoded.frames[0];
const filtered = await filterer.filter([frame]);
const encoded = await encoder.encode(filtered[0].frames[0]);
await encoder.flush();
await fs.writeFile("output.jpg", encoded.packets[0].data);
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment