import { seekVideo } from "./video"; export function captureAnalysisFrame( video: HTMLVideoElement, canvas: HTMLCanvasElement, width: number, height: number, ): ImageData { canvas.width = width; canvas.height = height; const context = canvas.getContext("2d", { willReadFrequently: true }); if (!context) throw new Error("Could not create the analysis canvas context."); context.drawImage(video, 0, 0, width, height); return context.getImageData(0, 0, width, height); } export async function createThumbnailUrl(video: HTMLVideoElement, maxWidth = 360): Promise { const scale = Math.min(1, maxWidth / video.videoWidth); const width = Math.max(1, Math.round(video.videoWidth * scale)); const height = Math.max(1, Math.round(video.videoHeight * scale)); const canvas = document.createElement("canvas"); canvas.width = width; canvas.height = height; const context = canvas.getContext("2d"); if (!context) throw new Error("Could not create thumbnail canvas context."); context.drawImage(video, 0, 0, width, height); const blob = await canvasToBlob(canvas, "image/webp", 0.82); return URL.createObjectURL(blob); } export async function extractFullResolutionFrame( video: HTMLVideoElement, time: number, type: "image/png" | "image/jpeg" | "image/webp" = "image/png", quality?: number, ): Promise { await seekVideo(video, time); const canvas = document.createElement("canvas"); canvas.width = video.videoWidth; canvas.height = video.videoHeight; const context = canvas.getContext("2d"); if (!context) throw new Error("Could not create full-resolution canvas context."); context.drawImage(video, 0, 0); return canvasToBlob(canvas, type, quality); } export async function canvasToBlob( canvas: HTMLCanvasElement, type: string, quality?: number, ): Promise { return new Promise((resolve, reject) => { canvas.toBlob( (blob) => { if (!blob) reject(new Error("Canvas export failed.")); else resolve(blob); }, type, quality, ); }); }