record.html 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>wgchen</title>
  6. <link rel="shortcut icon" href="#" />
  7. <meta name="keywords" content="https://wgchen.blog.csdn.net">
  8. <meta name="keywords" content="技术文章">
  9. <meta name="keywords" content="博客">
  10. <meta name="keywords" content="willem">
  11. <meta name="keywords" content="ycc">
  12. </head>
  13. <body>
  14. <video class="video" width="600px" controls></video>
  15. <button class="record-btn">record</button>
  16. <script>
  17. let btn = document.querySelector(".record-btn")
  18. console.log(navigator);
  19. console.log(navigator.mediaDevices);
  20. btn.addEventListener("click", async function () {
  21. let stream = await navigator.mediaDevices.getDisplayMedia({
  22. video: true
  23. })
  24. // 需要更好的浏览器支持
  25. const mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9") ?
  26. "video/webm; codecs=vp9" :
  27. "video/webm"
  28. let mediaRecorder = new MediaRecorder(stream, {
  29. mimeType: mime
  30. })
  31. let chunks = []
  32. mediaRecorder.addEventListener('dataavailable', function (e) {
  33. chunks.push(e.data)
  34. })
  35. mediaRecorder.addEventListener('stop', function () {
  36. let blob = new Blob(chunks, {
  37. type: chunks[0].type
  38. })
  39. let url = URL.createObjectURL(blob)
  40. let video = document.querySelector("video")
  41. video.src = url
  42. let a = document.createElement('a')
  43. a.href = url
  44. a.download = 'video.webm'
  45. a.click()
  46. })
  47. // 必须手动启动
  48. mediaRecorder.start()
  49. })
  50. </script>
  51. </body>
  52. </html>