Here is an implementation that extracts the current frame of HTMLVideoElement as an image.
Capture video frames using Canvas API and obtain them in Blob format.
API to use
-HTMLCanvasElement
- HTMLVideoElement -Promise API
implementation
interface OutputImage { type: "png" | "jpeg" | "webp"; quality?: number;}
interface TransferOptions { canvas: HTMLCanvasElement; video: HTMLVideoElement; output: OutputImage;}
interface TransferredImage { blob: Blob; width: number; height: number; type: string;}
function transferImage(options: TransferOptions): Promise<TransferredImage> { const { canvas, video, output } = options;
const height = video.videoHeight; const width = video.videoWidth;
return new Promise<TransferredImage>((resolve, reject) => { const ctx = canvas.getContext("2d"); if (!ctx) { reject(new Error("Context2D is not defined")); return; }
ctx.drawImage(video, 0, 0, width, height);
const type = `image/${output.type}`; const q = output.quality || 1;
function toBlob(blob: Blob | null) { if (blob) { resolve({ blob, width, height, type }); } else { reject(new Error("Failed to create blob from canvas")); } }
canvas.toBlob(toBlob, type, q); });}Points
- Get the video resolution with
video.videoHeightandvideo.videoWidth - Draw the current frame on Canvas with
ctx.drawImage(video, 0, 0, width, height) - Convert Canvas content to Blob with
canvas.toBlob - Wrap asynchronous processing with promises
Usage example
const video = document.querySelector('video');const canvas = document.createElement('canvas');
const image = await transferImage({ canvas, video, output: { type: 'png', quality: 1 }});
// image.blob を使って画像をダウンロードしたり、表示したりできるconst url = URL.createObjectURL(image.blob);summary
By combining HTMLVideoElement and CanvasAPI, you can extract any frame of a video as an image.
It can be used to generate thumbnails and implement video preview functions.
hsb.horse