Skip to content

Instantly share code, notes, and snippets.

@lisajamhoury
Created July 3, 2018 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lisajamhoury/c0a44d3b4ec1dd024859e3f5e1926fc9 to your computer and use it in GitHub Desktop.
Save lisajamhoury/c0a44d3b4ec1dd024859e3f5e1926fc9 to your computer and use it in GitHub Desktop.
<html>
<head>
</head>
<body>
<div id=thumbs>Loading video in background...</div>
<script>
function videoToImage(iSrc) {
let i = 0;
let video = document.createElement("video");
let thumbs = document.getElementById("thumbs");
video.addEventListener('loadeddata', function() {
thumbs.innerHTML = "";
video.currentTime = i;
}, false);
video.addEventListener('seeked', function() {
// now video has seeked and current frames will show
// at the time as we expect
this.generateThumbnail(i);
// when frame is captured, increase
i++;
// if we are not passed end, seek to next interval
if (i <= video.duration) {
// this will trigger another seeked event
video.currentTime = i;
}
else {
// DONE!, next action
console.log("done!");
}
}, false);
video.preload = "auto";
// video.src = "./assets/vid_white.webm";
video.src = iSrc;
video.generateThumbnail = function() {
let c = document.createElement("canvas");
let ctx = c.getContext("2d");
c.width = this.videoWidth;
c.height = this.videoHeight;
ctx.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
let img = new Image();
img.src = c.toDataURL('image/webp');
thumbs.appendChild(img);
}
}
videoToImage("./assets/vid_white.webm");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment