Skip to content

Instantly share code, notes, and snippets.

@myalban
Forked from ajardin/capture.html
Created February 19, 2019 08:56
Show Gist options
  • Save myalban/77ea64bb3163bd7ad5940675dd67ebba to your computer and use it in GitHub Desktop.
Save myalban/77ea64bb3163bd7ad5940675dd67ebba to your computer and use it in GitHub Desktop.
Capture a thumbnail from a video with JavaScript.
<!DOCTYPE html>
<head>
<script type='text/javascript'>
window.onload = function () {
var video = document.getElementById('videoId');
var canvas = document.getElementById('canvasId');
var img = document.getElementById('imgId');
video.addEventListener('play', function () {
canvas.style.display = 'none';
img.style.display = 'none';
}, false);
video.addEventListener('pause', function () {
canvas.style.display = 'block';
img.style.display = 'block';
draw(video, canvas, img);
}, false);
};
function draw(video, canvas, img) {
var context = canvas.getContext('2d');
context.drawImage(video, 0, 0, canvas.width, canvas.height);
var dataURL = canvas.toDataURL();
img.setAttribute('src', dataURL);
}
</script>
</head>
<body>
<video id="videoId" autoplay loop controls>
<source src="test.webm" type="video/webm">
<source src="test.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<canvas id="canvasId"></canvas>
<img id="imgId" src=""/>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment