ai-chatbot/lib/video-canvas/video-edit-mask.ts

19 lines
794 B
TypeScript

import sharp from "sharp";
import type { VideoEditRequest } from "./video-edit-contract";
export async function createVideoEditMask(
regions: NonNullable<VideoEditRequest["regions"]>,
width: number,
height: number,
) {
const pixels = Buffer.alloc(width * height * 3, 0);
for (const region of regions) {
const left = Math.max(0, Math.floor(region.x * width));
const top = Math.max(0, Math.floor(region.y * height));
const right = Math.min(width, Math.ceil((region.x + region.width) * width));
const bottom = Math.min(height, Math.ceil((region.y + region.height) * height));
for (let y = top; y < bottom; y++) pixels.fill(255, (y * width + left) * 3, (y * width + right) * 3);
}
return sharp(pixels, { raw: { width, height, channels: 3 } }).png().toBuffer();
}