79

I am attempting to create a thumbnail preview from a video file (mp4,3gp) from a form input type='file'. Many have said that this can be done server side only. I find this hard to believe since I just recently came across this Fiddle using HTML5 Canvas and Javascript.

Thumbnail Fiddle

The only problem is this requires the video to be present and the user to click play before they click a button to capture the thumbnail. I am wondering if there is a way to get the same results without the player being present and user clicking the button. For example: User click on file upload and selects video file and then thumbnail is generated. Any help/thoughts are welcome!

13
  • The player obviously has to be present, as that is what is producing the image for capture.
    – vogomatix
    Commented May 13, 2014 at 20:25
  • 3
    you can hide the player using css, and call videoTag.play() to start it playing. i recommend jumping 18 seconds in, waiting until it shows, and then sending it to a canvas drawImage routine. i turned a folder of movies into a thumbnail gallery in chrome this way, so i can assure you it works.
    – dandavis
    Commented May 13, 2014 at 20:47
  • 1
    If the video uses a codec that the current browser supports, you can handle the dropped/selected video file, get an object URL for the video file, and set that value to the src attribute of a video element. Commented May 13, 2014 at 21:17
  • 2
    I've recently been working on a plug-in that addresses the items in your question (thumbnail generation from <video>s, conversion of Blob/File to a <video>, etc). Have a look, and perhaps it will prove to be useful for you. github.com/rnicholus/frame-grab.js Commented May 15, 2014 at 4:33
  • 2
    frame-grab's features are 100% client-side. For example if you include a file input element, or a drop zone on your page. When your user selects/drops a video file, you can pass the Blob from the file input/drop event on to frab-grab's blob_to_video method, get a <video> and feed that into a frame-grab instance, where you can have images generated via the various workflows/methods exposed in frame-grab's API. This all happens in the browser, nothing is sent to the server. I encourage you to ask questions or leave feature requests in the github repo, and we can discuss more there. Commented May 15, 2014 at 15:05

8 Answers 8

90

Canvas.drawImage must be based on html content.

source

here is a simplier jsfiddle

//and code
function capture(){
    var canvas = document.getElementById('canvas');
    var video = document.getElementById('video');
    canvas.getContext('2d').drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
}

The advantage of this solution is that you can select the thumbnail you want based on the time of the video.

4
  • 15
    Is their any way to do this without loading video on document Commented Aug 28, 2017 at 8:47
  • 9
    I only can get video size 0 0 and white image. Commented Sep 26, 2017 at 16:02
  • The thumbnail is trimmed from bottom, I can't get the full image Commented Sep 11, 2021 at 8:52
  • @wajdi_jurry The video size is loaded from the metadata and so you may need to add an event handler to the 'loadedmetadata' event and fetch the video width and height from there. Once you've got that you should then be able to resize the canvas and draw the image.
    – Ben Short
    Commented Sep 11, 2021 at 18:01
51

Recently needed this so I wrote a function, to take in a video file and a desired timestamp, and return an image blob at that time of the video.

Sample Usage:

try {
    // get the frame at 1.5 seconds of the video file
    const cover = await getVideoCover(file, 1.5);
    // print out the result image blob
    console.log(cover);
} catch (ex) {
    console.log("ERROR: ", ex);
}

Function:

function getVideoCover(file, seekTo = 0.0) {
    console.log("getting video cover for file: ", file);
    return new Promise((resolve, reject) => {
        // load the file to a video player
        const videoPlayer = document.createElement('video');
        videoPlayer.setAttribute('src', URL.createObjectURL(file));
        videoPlayer.load();
        videoPlayer.addEventListener('error', (ex) => {
            reject("error when loading video file", ex);
        });
        // load metadata of the video to get video duration and dimensions
        videoPlayer.addEventListener('loadedmetadata', () => {
            // seek to user defined timestamp (in seconds) if possible
            if (videoPlayer.duration < seekTo) {
                reject("video is too short.");
                return;
            }
            // delay seeking or else 'seeked' event won't fire on Safari
            setTimeout(() => {
              videoPlayer.currentTime = seekTo;
            }, 200);
            // extract video thumbnail once seeking is complete
            videoPlayer.addEventListener('seeked', () => {
                console.log('video is now paused at %ss.', seekTo);
                // define a canvas to have the same dimension as the video
                const canvas = document.createElement("canvas");
                canvas.width = videoPlayer.videoWidth;
                canvas.height = videoPlayer.videoHeight;
                // draw the video frame to canvas
                const ctx = canvas.getContext("2d");
                ctx.drawImage(videoPlayer, 0, 0, canvas.width, canvas.height);
                // return the canvas image as a blob
                ctx.canvas.toBlob(
                    blob => {
                        resolve(blob);
                    },
                    "image/jpeg",
                    0.75 /* quality */
                );
            });
        });
    });
}
8
  • Re the use of setTimeout, could the problem be that you're adding the event listener after you do the seek? Might make sense to add the listener first, then cause the seek to happen. Same with the loadedmetadata
    – xaphod
    Commented Sep 29, 2020 at 17:48
  • @xaphod I remember I tried even with setTimeout 0, which pushes the callback onto the event queue, still does not make it reliable enough on safari. It needs an actual delay. Maybe just Safari being too slow in loading the video? Have you been able to verify your suggestions on Safari?
    – WSBT
    Commented Sep 30, 2020 at 18:11
  • on my iMac with safari stable latest, it didn't need any setTimeout -- or at least, the thumbnail generated fine. That's with having the listeners added before the seek is requested.
    – xaphod
    Commented Sep 30, 2020 at 20:50
  • @xaphod I'm not on Safari 14 yet, but without set timeout, about 8 out of 10 videos would be fine, some occasional videos would fail for no unknown reason
    – WSBT
    Commented Sep 30, 2020 at 22:15
  • Thank you for the info. I’ll leave it in then to be safe.
    – xaphod
    Commented Oct 1, 2020 at 2:26
33

Recently needed this and did quite some testing and boiling it down to the bare minimum, see https://codepen.io/aertmann/pen/mAVaPx

There are some limitations where it works, but fairly good browser support currently: Chrome, Firefox, Safari, Opera, IE10, IE11, Android (Chrome), iOS Safari (10+).

 video.preload = 'metadata';
 video.src = url;
 // Load video in Safari / IE11
 video.muted = true;
 video.playsInline = true;
 video.play();
2
  • 1
    Your codepen seems to be good, but when I try to upload > 100mb iPhone portrait shoot mode videos sometimes the screenshots are blank or sometimes they dont get uploaded only. FYI, I want canvas size to be 170x100. Had you faced the similar issues for iPhone portrait mode videos?
    – vbjain
    Commented Mar 5, 2018 at 11:03
  • @vbjain No can't say that I have, sorry. Commented Mar 6, 2018 at 17:36
23

The easiest way to display a thumbnail is using the <video> tag itself.

<video src="http://www.w3schools.com/html/mov_bbb.mp4"></video>

Use #t in the URL, if you want the thumbnail of x seconds.

E.g.:

<video src="http://www.w3schools.com/html/mov_bbb.mp4#t=5"></video>

Make sure that it does not include any attributes like autoplay or controls and it should not have a source tag as a child element.

With a little bit of JavaScript, you may also be able to play the video, when the thumbnail has been clicked.

document.querySelector('video').addEventListener('click', (e) => {
  if (!e.target.controls) { // Proceed, if there are no controls
    e.target.src = e.target.src.replace(/#t=\d+/g, ''); // Remove the time, which is set in the URL
    e.target.play(); // Play the video
    e.target.controls = true; // Enable controls
  }
});
<video src="http://www.w3schools.com/html/mov_bbb.mp4#t=5"></video>

3
  • 6
    Also, the question is for video coming from file input, not url.
    – ManUtopiK
    Commented Jan 29, 2022 at 0:02
  • this method is eating up a lot of bandwidth
    – PauAI
    Commented Apr 9 at 8:52
  • 1
    @ManUtopiK We can create a objectUrl from the file and pass to the src attribute. Commented Jun 8 at 14:38
21

You can use this function that I've written. You just need to pass the video file to it as an argument. It will return the dataURL of the thumbnail(i.e image preview) of that video. You can modify the return type according to your need.

const generateVideoThumbnail = (file: File) => {
  return new Promise((resolve) => {
    const canvas = document.createElement("canvas");
    const video = document.createElement("video");

    // this is important
    video.autoplay = true;
    video.muted = true;
    video.src = URL.createObjectURL(file);

    video.onloadeddata = () => {
      let ctx = canvas.getContext("2d");

      canvas.width = video.videoWidth;
      canvas.height = video.videoHeight;

      ctx.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
      video.pause();
      return resolve(canvas.toDataURL("image/png"));
    };
  });
};

Please keep in mind that this is a async function. So make sure to use it accordingly.

For instance:

const handleFileUpload = async (e) => {
  const thumbnail =  await generateVideoThumbnail(e.target.files[0]);
  console.log(thumbnail)
}
1
  • 1
    Please make sure to call URL.revokeObjectURL(file); after you're done (eg, in onloadedmetadata, otherwise it would impact your page performance due to memory overload (specially with videos), because URL.createObjectURL(file); creates new object url every time and memory needs to be released for page to work properly.
    – Sayed
    Commented Apr 20 at 23:54
1

With jQuery Lib you can use my code here. $video is a Video element.This function will return a string

function createPoster($video) {
    //here you can set anytime you want
    $video.currentTime = 5;
    var canvas = document.createElement("canvas");
    canvas.width = 350;
    canvas.height = 200;
    canvas.getContext("2d").drawImage($video, 0, 0, canvas.width, canvas.height);
    return canvas.toDataURL("image/jpeg");;
}

Example usage:

$video.setAttribute("poster", createPoster($video));
0

I was coming across this issue in a nextJS site where I was using the mozilla MediaRecorder to record a video client side in browser. On mobile the first frame was always empty and the hacks to use a few milliseconds in weren't helpful since my video was coming from the MediaRecorder blob (rather than being streamed from a file).

If it helps anyone looking to do this in react and utilising "react-media-recorder" below worked perfectly for me taking a snapshot of the video after it starts recording and then using this as the thumbnail as the "poster" attribute.

import React, { useEffect, useRef, useState } from "react";
import { useReactMediaRecorder } from "react-media-recorder";

export default function VideoRecorder() {
  const { status, startRecording, stopRecording, mediaBlobUrl, error } =
    useReactMediaRecorder({ video: true });

  // State to hold the thumbnail URL
  const [thumbnailUrl, setThumbnailUrl] = useState(null);

  // Reference to the video element
  const videoRef = useRef(null);

  // Reference to the canvas element
  const canvasRef = useRef(null);

  useEffect(() => {
    if (videoRef.current) {
      videoRef.current.addEventListener("playing", captureSnapshot);
    }

    return () => {
      if (videoRef.current) {
        videoRef.current.removeEventListener("playing", captureSnapshot);
      }
    };
  }, []);

  const captureSnapshot = () => {
    /* Get canvas context */
    const ctx = canvasRef.current.getContext("2d");

    /* Set canvas dimensions to match video */
    canvasRef.current.width = videoRef.current.videoWidth;
    canvasRef.current.height = videoRef.current.videoHeight;

    /* Draw current video frame onto the canvas */
    ctx.drawImage(videoRef.current, 0, 0);

    /* Export canvas content to data URL and set as thumbnail */
    setThumbnailUrl(canvasRef.current.toDataURL("image/jpeg", 0.95));
  };

  if (error) {
    return <p>Error: {error}</p>;
  }

  return (
    <div>
      <p>{status}</p>
      <button onClick={startRecording} disabled={status === "recording"}>
        Start Recording
      </button>
      <button onClick={stopRecording} disabled={status !== "recording"}>
        Stop Recording
      </button>
      {mediaBlobUrl && (
        <video
          ref={videoRef}
          poster={thumbnailUrl}
          src={mediaBlobUrl}
          controls
          autoPlay
          loop
          style={{ maxWidth: "100%", maxHeight: "100%", objectFit: "contain" }}
        />
      )}
      <canvas ref={canvasRef} style={{ display: "none" }}></canvas>
    </div>
  );
}
-1

I recently stumbled on the same issue and here is how I got around it.

firstly it will be easier if you have the video as an HTML element, so you either have it in the HTML like this

<video src="http://www.w3schools.com/html/mov_bbb.mp4"></video>

or you take from the input and create an HTML element with it.

The trick is to set the start time in the video tag to the part you want to seek and have as your thumbnail, you can do this by adding #t=1.5 to the end of the video source.

<video src="http://www.w3schools.com/html/mov_bbb.mp4#t=1.5"></video>

where 1.5 is the time you want to seek and get a thumbnail of.

This, however, makes the video start playing from that section of the video so to avoid that we add an event listener on the video's play button(s) and have the video start from the beginning by setting video.currentTime = 0

const video = document.querySelector('video');

video.addEventListener('click', (e)=> {
video.currentTime = 0 ; 
video.play();
})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.