Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to get thumbnail image from video file in javascript

this answer is awesome:

https://stackoverflow.com/questions/23640869/create-thumbnail-from-video-file-via-file-input
Comment

get thumbnail from video js

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 */
                );
            });
        });
    });
}
Comment

how to create thumbnail image from video in javascript

//Function to generate video thumbnail
const generateVideoThumbnail = (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"));
      };
    });
  };

//using function
const getVideoTb = async() => {
  const thumbnail =  await generateVideoThumbnail(item.file);
      console.log(thumbnail)
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js check if undefined 
Javascript :: how to code print in javascript 
Javascript :: create a loop that runs through each item in an array 
Javascript :: array shift javascript 
Javascript :: generate a random number between min and max 
Javascript :: .on click jquery 
Javascript :: javascript compare two arrays of objects 
Javascript :: add parameters ajax request 
Javascript :: script tag inside react component 
Javascript :: how to delete duplicate elements in an array in javascript 
Javascript :: array javascript some vs every 
Javascript :: faker random from array 
Javascript :: sample docker for node js 
Javascript :: next js page loader 
Javascript :: react-native-config 
Javascript :: filter duplicates multidimensional array javascript 
Javascript :: swap function javascript 
Javascript :: send csrf token ajax laravel 
Javascript :: disable button 
Javascript :: how to change mui ripple color 
Javascript :: disable button based on condition angular 
Javascript :: reverse string in js 
Javascript :: url validation in formcontrol angular 8 
Javascript :: js map through array and return array of objects 
Javascript :: javascript debounce 
Javascript :: javascript array remove middle 
Javascript :: javascript show page 
Javascript :: javascript round off 
Javascript :: debug.xcconfig: unable to open file react native 
Javascript :: even or odd in javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =