js 调用摄像头录像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!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>

  

posted @   到此灬一游丿  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
历史上的今天:
2020-01-30 css 常用属性
2020-01-30 es6 语法
2020-01-30 vuex 基本语法
2020-01-30 git 常用命令
2020-01-30 css选择器
点击右上角即可分享
微信分享提示