Created
July 21, 2017 16:43
-
-
Save garystorey/1265dc394de4a2b9da4051354466e1ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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