JS gets the first frame of the video

Source: Internet
Author: User

<! DOCTYPE html>

<meta charset= "UTF-8" >
<title>capture screen</title>
<body>
<video id= "video" controls= "Controls" >
<source src= "Img/test.mp4" >
</video>
<div id= "Output" ></div>
<script type= "Text/javascript" >
(function () {
var video, output;
var scale = 0.8;
var initialize = function () {
Output = document.getElementById ("Output");
Video = document.getElementById ("video");
Video.addeventlistener (' Loadeddata ', captureimage); //used to add an event handle to the specified element.
};

var captureimage = function () {
var canvas = document.createelement ("Canvas"); Create a canvas
Canvas.width = video.videowidth * scale;
Canvas.height = video.videoheight * scale;
Canvas.getcontext (' 2d '). DrawImage (video, 0, 0, canvas.width, canvas.height); GetContext: Setting the canvas environment; DrawImage: Drawing

var img = document.createelement ("img");
IMG.SRC = Canvas.todataurl ("Image/png"); Get the URL of a picture
Output.appendchild (IMG);
};

Initialize ();
})();
</script>
</body>

JS gets the first frame of the video

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.