go2rtc
时间: 2025-06-02 08:33:45 浏览: 44
### go2rtc 项目介绍
go2rtc 是一个基于 WebRTC 的开源音视频流媒体服务器,支持多种输入源(如 RTSP、RTMP 和本地文件),并将这些流转换为低延迟的 WebRTC 流。它的主要目标是提供一种简单的方法来将传统的 IP 摄像头或其他音视频设备接入到现代浏览器中[^2]。
#### 功能特点
- **多协议支持**:可以接收来自 RTSP、RTMP 或本地文件的输入流。
- **WebRTC 输出**:通过 WebRTC 提供低延迟的实时音视频传输。
- **跨平台兼容性**:可以在 Linux、Windows 和 macOS 上运行。
- **易于扩展**:可以通过插件机制增加新的功能或适配器。
- **Home Assistant 集成**:能够轻松集成到 Home Assistant 中作为附加组件使用[^2]。
---
### 使用指南
#### 安装方法
1. 如果您已经安装了 Docker,则可以直接拉取官方镜像并启动容器:
```bash
docker run --rm \
-p 8083:8083 \
-v /etc/go2rtc:/config \
alexanderjonasson/go2rtc
```
2. 对于 Home Assistant 用户,可以通过以下方式添加 go2rtc 组件:
- 进入 Home Assistant 设置页面 -> “附加存储库”,添加仓库地址 `https://github.com/AlexxIT/hassio-addons`。
- 找到并安装名为 `go2rtc` 的附加组件。
- 启动后,访问其配置界面完成进一步设置[^2]。
#### 基本配置
默认情况下,go2rtc 将监听端口 `8083` 并暴露 REST API 接口以及 WebRTC 服务。您可以编辑配置文件 `/config/config.yaml` 来调整参数。例如:
```yaml
streams:
camera_1:
url: rtsp://username:password@ip_address/stream_path
codec_copy: true
```
此配置表示从指定的 RTSP 地址读取摄像头流,并将其命名为 `camera_1`[^2]。
#### 示例代码
下面是一个简单的 HTML 页面示例,展示如何连接到 go2rtc 并播放 WebRTC 流:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>go2rtc Example</title>
</head>
<body>
<video id="remoteVideo" autoplay playsinline></video>
<script>
const configuration = {
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
};
let pc;
async function start() {
try {
pc = new RTCPeerConnection(configuration);
// Add event listeners...
pc.ontrack = (event) => {
document.getElementById('remoteVideo').srcObject = event.streams[0];
};
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
// Simulate signaling process here by sending the SDP to server.
console.log(pc.localDescription.sdp);
// Assume we receive an answer from the server and set it as remote description.
const answerSdp = /* receivedAnswer */;
const answer = new RTCSessionDescription({ type: "answer", sdp: answerSdp });
await pc.setRemoteDescription(answer);
} catch (error) {
console.error(error);
}
}
start();
</script>
</body>
</html>
```
在此示例中,我们创建了一个 RTCPeerConnection 实例并与 go2rtc 交换 SDP 描述符以建立连接[^3]。
---
###
阅读全文
相关推荐











