Last active
October 2, 2019 21:48
-
-
Save westc/f6de681820d78df64c01e10bfd03f985 to your computer and use it in GitHub Desktop.
A function to grab the frame at a specific point within a video.
This file contains 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
Here's a slightly improved version ready to be imported with webpack or parceljs