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 :: js local storage 
Javascript :: react native dimensions 
Javascript :: next auth session callback 
Javascript :: jquery validation on click 
Javascript :: how to create node js server 
Javascript :: isfunction javascript 
Javascript :: javascript sort associative array 
Javascript :: javascript read text file from url 
Javascript :: javascript timing events 
Javascript :: flutter or react native 
Javascript :: javascript create folder 
Javascript :: deprecationwarning: mongoose 
Javascript :: How to initialize select2 dynamically 
Javascript :: months js 
Javascript :: jquery get position of element 
Javascript :: adding a timer in a quiz game 
Javascript :: new map js 
Javascript :: async arrow function in javascript 
Javascript :: javascript dom to image 
Javascript :: move item to end of array for of 
Javascript :: javascript get type of var 
Javascript :: how to check if array 
Javascript :: window.scrollTo Id sample code 
Javascript :: check-if-a-javascript-string-is-a-url 
Javascript :: what is the difference between let and const in javascript 
Javascript :: disable link react 
Javascript :: ckeditor get instance from textarea 
Javascript :: react history listen get previous location 
Javascript :: javaScript setMinutes() Method 
Javascript :: anagram 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =