Skip to content

Instantly share code, notes, and snippets.

@ryanwild
Forked from westc/getVideoImage.js
Created May 5, 2017 11:29
Show Gist options
  • Save ryanwild/d60eedd0a1c27e78576868efd0b8b0b4 to your computer and use it in GitHub Desktop.
Save ryanwild/d60eedd0a1c27e78576868efd0b8b0b4 to your computer and use it in GitHub Desktop.
A function to grab the frame at a specific point within a video.
function getVideoImage(path, secs, callback) {
var me = this, video = document.createElement('video');
video.onloadedmetadata = function() {
if ('function' === typeof secs) {
secs = secs(this.duration);
}
this.currentTime = Math.min(Math.max(0, (secs < 0 ? this.duration : 0) + secs), this.duration);
};
video.onseeked = function(e) {
var canvas = document.createElement('canvas');
canvas.height = video.videoHeight;
canvas.width = video.videoWidth;
var ctx = canvas.getContext('2d');
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
var img = new Image();
img.src = canvas.toDataURL();
callback.call(me, img, this.currentTime, e);
};
video.onerror = function(e) {
callback.call(me, undefined, undefined, e);
};
video.src = path;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment