Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript generate video thumbnail from video url

I'm going to generate video thumbnail from video url in javascript. I need this done via ajax. So I followed this approach.

var src = thumbnail; ///video url not youtube or vimeo,just video on server
var video = document.createElement('video');

video.src = src;

video.width = 360;
video.height = 240;

var canvas = document.createElement('canvas');
canvas.width = 360; 
canvas.height = 240;
var context = canvas.getContext('2d');

context.drawImage(video, 0, 0, canvas.width, canvas.height);
var dataURI = canvas.toDataURL('image/jpeg');
html += '<figure>';
html += '<img src="' + dataURI + '' + '" alt="' + item.description + '" />';
html += '<figurecaption>'+item.description+'</figurecaption>'
html += '</figure>';

But what I get is just black image. I guess it's because load problem but can't find a solution. Looking forward to hearing from you.

Regards.

like image 509
Softalent Avatar asked Oct 20 '16 01:10

Softalent


People also ask

How do I get a video thumbnail in HTML?

Approach: Sometimes the user wants to display a specific image of his choice as the thumbnail of the video. This can be simply done by using the poster attribute. All you have to do is create a poster attribute in the video tag and place the URL of the thumbnail image in it.


1 Answers

You can install the npm: video-metadata-thumbnails, then use it like this:

import { getMetadata, getThumbnails } from 'video-metadata-thumbnails';

const thumbnails = await getThumbnails(blob, {
  quality: 0.6
});
console.log('Thumbnails: ', thumbnails);
like image 139
Wayne Avatar answered Oct 18 '22 10:10

Wayne