logo hsb.horse
← Back to blog index

Blog

TypeScript implementation to generate images from HTMLVideoElement

A TypeScript implementation that uses Canvas and VideoElement to extract the current frame of a video as an image. Organized the method of generating blobs based on promises.

Published:

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

  1. Get the video resolution with video.videoHeight and video.videoWidth
  2. Draw the current frame on Canvas with ctx.drawImage(video, 0, 0, width, height)
  3. Convert Canvas content to Blob with canvas.toBlob
  4. 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.