首页 > 其他分享 >js 调用摄像头录像

js 调用摄像头录像

时间:2023-01-30 22:36:38浏览次数:46  
标签:function recoder stream js 录像 link var data 摄像头

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>调用摄像头录像</title>
</head>

<body>
    <video width="640" height="480" id="video" muted autoplay></video>
    <button onclick="start()">开始</button>
    <button type="button" onclick="stop()">结束</button>
    <script>
        var recoder;
        var data = []
        function start() {
            navigator.mediaDevices.getUserMedia({ video: true, audio: true }).then(stream => {
                document.getElementById("video").srcObject = stream;
                recoder = new MediaRecorder(stream);

                recoder.ondataavailable = function (e) {
                    data.push(e.data)
                }
                recoder.onstop = function () {                   
                    this.stream.getTracks().forEach(track => track.stop());
                    var blob = new Blob(data, { type: this.mimeType });
                    var link = document.createElement("a")
                    link.href = URL.createObjectURL(blob)
                    link.download = new Date().getTime() + ".mp4";
                    document.body.appendChild(link)
                    link.click()
                    URL.revokeObjectURL(link.href)
                    link.remove()
                }
                recoder.start()
            })
        }
        function stop() {
            recoder.stop();
            data = []
        }
    </script>
</body>

</html>

  

标签:function,recoder,stream,js,录像,link,var,data,摄像头
From: https://www.cnblogs.com/finghi/p/17077426.html

相关文章