WebRTC(Web Real - Time Communication)是一项强大的 Web 技术,可实现浏览器之间的实时通信,包括音视频流的传输。下面将详细介绍如何使用 WebRTC 实现实时滤镜与录屏功能。

1. 项目初始化

创建一个 HTML 文件,例如 index.html,并引入必要的元素和样式。

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

<head>
    <meta charset="UTF - 8">
    <meta name="viewport" content="width=device - width, initial - scale = 1.0">
    <title>WebRTC 实时滤镜与录屏</title>
    <style>
        video {
            width: 640px;
            height: 480px;
            margin: 10px;
        }
    </style>
</head>

<body>
    <video id="localVideo" autoplay muted></video>
    <video id="filteredVideo" autoplay></video>
    <button id="startRecording">开始录屏</button>
    <button id="stopRecording">停止录屏</button>
    <a id="downloadLink" download="recorded_video.webm">下载录屏</a>
    <script src="script.js"></script>
</body>

</html>
  • 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.
2. 获取用户媒体流

script.js 文件中,首先获取用户