← All Insights

How to Get Video Thumbnails with JavaScript

When users upload videos — for a profile cover, a post, or a media library — it is useful to generate a thumbnail on the client side before the upload completes. This avoids a round-trip to the server and gives instant visual feedback.

The HTML5 Video element and Canvas API make this possible entirely in the browser, without any external libraries.


How It Works

The process has five steps:

  1. Create a Blob URL from the selected file
  2. Load it into a hidden video element
  3. Wait for metadata to load so dimensions are known
  4. Seek to the desired timestamp
  5. Draw that frame to a canvas and export it as a base64 image

HTML

<input type="file" id="video-input" accept="video/mp4,video/*" />
<img id="thumbnail" alt="Video thumbnail" style="max-width:320px;display:none;" />

JavaScript

function getVideoThumbnail(file, seekTo = 0.0) {
  return new Promise((resolve, reject) => {
    const video = document.createElement('video');
    video.preload = 'metadata';
    video.muted = true;
    video.playsInline = true;

    const url = URL.createObjectURL(file);
    video.src = url;

    video.addEventListener('loadedmetadata', () => {
      video.currentTime = seekTo;
    });

    video.addEventListener('seeked', () => {
      const canvas = document.createElement('canvas');
      canvas.width = video.videoWidth;
      canvas.height = video.videoHeight;

      const ctx = canvas.getContext('2d');
      ctx.drawImage(video, 0, 0, canvas.width, canvas.height);

      const dataURL = canvas.toDataURL('image/png', 0.60);
      URL.revokeObjectURL(url);
      resolve(dataURL);
    });

    video.addEventListener('error', reject);
  });
}

document.getElementById('video-input').addEventListener('change', async e => {
  const file = e.target.files[0];
  if (!file) return;

  try {
    const thumbnail = await getVideoThumbnail(file, 1.0);
    const img = document.getElementById('thumbnail');
    img.src = thumbnail;
    img.style.display = 'block';
  } catch (err) {
    console.error('Failed to generate thumbnail:', err);
  }
});

Key Details

  • seekTo parameter: The default of 0.0 captures the very first frame. For videos that open on a black screen, seek to a later timestamp like 1.0 or 2.0 for a more representative frame.
  • URL.revokeObjectURL: Always call this after you are done with the Blob URL to release the memory reference.
  • toDataURL quality: The second argument to toDataURL controls JPEG quality (0–1). It has no effect on PNG output, but a value of 0.60 works well if you switch to image/jpeg for smaller thumbnails.
  • Browser support: The Canvas API and HTML5 Video are supported in all modern browsers. No polyfills are needed.

Using with React

import { useState } from 'react';

function VideoUpload() {
  const [thumbnail, setThumbnail] = useState(null);

  const handleChange = async e => {
    const file = e.target.files[0];
    if (!file) return;
    const dataURL = await getVideoThumbnail(file, 1.0);
    setThumbnail(dataURL);
  };

  return (
    <div>
      <input type="file" accept="video/*" onChange={handleChange} />
      {thumbnail && <img src={thumbnail} alt="Thumbnail" style={{ maxWidth: 320 }} />}
    </div>
  );
}

Conclusion

Generating a video thumbnail on the client requires no server calls and no libraries — just the standard HTML5 APIs available in every modern browser. The async wrapper makes it easy to integrate into any upload flow, whether vanilla JavaScript or a React component.