Skip to content

Instantly share code, notes, and snippets.

@garystorey
Created July 21, 2017 16:43
Show Gist options
  • Save garystorey/1265dc394de4a2b9da4051354466e1ce to your computer and use it in GitHub Desktop.
Save garystorey/1265dc394de4a2b9da4051354466e1ce to your computer and use it in GitHub Desktop.
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