I'm trying to generate a screenshot of a video. The goal is to generate an image that is identical to the first frame of the video. However, I'm facing an issue with the resulting image being stretched.
This image displays the video on the left, paused at the very beginning - and the generated image on the right:
Notice the grey area at the bottom? It has been stretched to a taller height than that of the video.
I am using the following code the generate the screenshot:
Example Markup:
<video id="video" height="300" width="320" preload="auto">
<source src="http://domain.com/video.mp4">
</video>
<canvas id="canvas" height="300" width="320"></canvas>
Script:
// Grab an existing video element, 300px tall, 320px wide.
var video = document.getElementById('video');
// Grab existing canvas, 300px tall, 320px wide.
var canvas = document.getElementById('canvas');
// Wait until video has loaded
video.addEventListener('loadeddata', function() {
var context = canvas.getContext('2d');
// Render using same dimensions.
context.drawImage(video, 0, 0, 320, 300);
}, false);
Could someone please advise me on how to avoid this stretching?