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

how to set thumbnail for videos in javascript

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

PREVIOUS NEXT
Code Example
Javascript :: round decimal js 
Javascript :: keydown events 
Javascript :: axios default baseurl conditional environment 
Javascript :: javascript function to open file browser 
Javascript :: javascript get element using id and class name 
Javascript :: javascript stop execution 
Javascript :: jquery list all event listeners 
Javascript :: how do i check if JQuery checkbox is checked 
Javascript :: Error R10 (Boot timeout) - Web process failed to bind to $PORT within 60 seconds of launch 
Javascript :: npm fund error 
Javascript :: do while javascript 
Javascript :: downgrade nodejs with nvm 
Javascript :: array.unshift in javascript 
Javascript :: express.urlencoded extended true or false 
Javascript :: todashcase javascript 
Javascript :: javascript validate string with regex 
Javascript :: i18n vue cli 
Javascript :: tinymce update textarea value using jquery 
Javascript :: js looping through array 
Javascript :: how to make back button react 
Javascript :: add array to array javascript 
Javascript :: javascript onkeydown 
Javascript :: javascript document.createElement add function 
Javascript :: comparing two arrays in javascript returning differences 
Javascript :: countdown timer javascript stack overflow 
Javascript :: vue.js 
Javascript :: dynamodb get all items nodejs 
Javascript :: js get file location 
Javascript :: css class list 
Javascript :: react render after data loaded 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =