1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- * {
- margin: 0;
- padding: 0;
- }
- #video {
- width: 100%;
- height: 100%;
- object-fit: cover;
- }
- </style>
- <script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script>
- <script>
- // VConsole 默认会挂载到 `window.VConsole` 上
- var vConsole = new window.VConsole();
- </script>
- </head>
- <body>
- <video id="video" autoplay></video>
- <button id="saveButton">保存</button>
- <script>
- const video = document.getElementById('video');
- let mediaRecorder;
- let recordedBlobs;
- if (navigator.mediaDevices.getUserMedia) {
- navigator.mediaDevices.getUserMedia({
- video: {
- width: { ideal: 1920 },
- height: { ideal: 1080 }
- }
- })
- .then(function (stream) {
- video.srcObject = stream;
- mediaRecorder = new MediaRecorder(stream);
- console.log('mediaRecorder : ', mediaRecorder.state, mediaRecorder)
- mediaRecorder.ondataavailable = event => {
- console.log('event : ', event)
- if (event.data && event.data.size > 0) {
- recordedBlobs.push(event.data);
- }
- };
- mediaRecorder.start(1000);
- console.log('mediaRecorder : ', mediaRecorder.state, mediaRecorder)
- recordedBlobs = [];
- })
- .catch(function (err) {
- console.log("Something went wrong!", err);
- });
- }
- // 在用户点击按钮时保存录制的视频
- document.querySelector('#saveButton').addEventListener('click', () => {
- mediaRecorder.stop();
- console.log('recordedBlobs : ', recordedBlobs)
- const blob = new Blob(recordedBlobs, { type: 'video/webm' });
- const url = window.URL.createObjectURL(blob);
- const a = document.createElement('a');
- a.style.display = 'none';
- a.href = url;
- a.download = 'test.webm';
- document.body.appendChild(a);
- a.click();
- setTimeout(() => {
- document.body.removeChild(a);
- window.URL.revokeObjectURL(url);
- }, 100);
- });
- </script>
- </body>
- </html>
|