1. 环境概览
go2rtc 是一款高性能流媒体代理,支持 RTSP/RTMP/WebRTC 等多种协议,可将摄像头流统一进出。本教程将用 Docker 部署,并提供一份可直接执行的安装脚本。
2. 安装前准备
2.1 清理可能存在的无效 APT 源
如果系统内曾经添加过 Docker 官方源(download.docker.com),因其国内访问不稳定,会导致 apt update 失败。请在执行安装脚本前手动删除:
sudo rm -f /etc/apt/sources.list.d/docker*.list2.2 确认 Docker 服务正常
sudo systemctl status docker
docker info3. 一键安装
将下面脚本保存为 install_go2rtc.sh,赋予执行权限后运行即可。
#!/bin/bash
set -e
# 颜色输出
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
info() { echo -e "${GREEN}[INFO]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }
# 检查 sudo 权限
if ! sudo -v &>/dev/null; then
error "当前用户无 sudo 权限,请使用 root 或具备 sudo 的用户执行。"
fi
# ---------- 1. 更换 APT 源(清华 ubuntu-ports,适配 aarch64) ----------
info "更换 APT 源为清华镜像..."
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak.$(date +%Y%m%d%H%M%S)
sudo tee /etc/apt/sources.list > /dev/null <<'EOF'
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse
EOF
info "更新 APT 缓存..."
sudo apt update
# ---------- 2. 安装必要工具 ----------
info "安装 curl wget..."
sudo apt install -y curl wget
# ---------- 3. 配置 Docker 镜像加速 ----------
info "配置 Docker 镜像加速器..."
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json > /dev/null <<'EOF'
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://dockerproxy.com",
"https://docker.nju.edu.cn"
]
}
EOF
info "重启 Docker 服务..."
sudo systemctl restart docker || warn "Docker 重启可能失败,请稍后手动重启"
# ---------- 4. 安装 Docker Compose v1(aarch64 二进制) ----------
if ! command -v docker-compose &>/dev/null; then
info "安装 Docker Compose v1..."
COMPOSE_VERSION="1.29.2"
COMPOSE_URL="https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-Linux-aarch64"
COMPOSE_URL_ALT="https://ghproxy.com/${COMPOSE_URL}"
if ! sudo curl -L "${COMPOSE_URL}" -o /usr/local/bin/docker-compose; then
warn "官方 GitHub 下载失败,尝试使用 ghproxy 加速..."
sudo curl -L "${COMPOSE_URL_ALT}" -o /usr/local/bin/docker-compose || error "docker-compose 下载失败"
fi
sudo chmod +x /usr/local/bin/docker-compose
info "docker-compose 安装成功: $(docker-compose --version)"
else
info "docker-compose 已安装: $(docker-compose --version)"
fi
# ---------- 5. 创建 go2rtc 工作目录与基础配置 ----------
WORKDIR="$HOME/go2rtc"
mkdir -p "$WORKDIR" && cd "$WORKDIR"
# 基础配置文件(稍后您需修改为自己的摄像头流)
cat > go2rtc.yaml <<'EOF'
# 在此处添加您的摄像头流,格式:
# streams:
# camera1: rtsp://username:password@ip:port/path
streams: {}
EOF
# docker-compose 文件
cat > docker-compose.yml <<'EOF'
version: '3.8'
services:
go2rtc:
image: alexxit/go2rtc
container_name: go2rtc
restart: unless-stopped
network_mode: host # 使用 host 网络以减少延迟
environment:
- TZ=Asia/Shanghai
volumes:
- ./go2rtc.yaml:/config/go2rtc.yaml
EOF
info "工作目录已创建: $WORKDIR"
# ---------- 6. 启动 go2rtc ----------
if command -v docker-compose &>/dev/null; then
info "使用 docker-compose 启动..."
if ! docker-compose up -d; then
warn "docker-compose 启动失败,尝试直接使用 docker run..."
FALLBACK=true
else
info "go2rtc 已通过 docker-compose 启动"
docker-compose ps
exit 0
fi
else
FALLBACK=true
fi
if [ "$FALLBACK" = true ]; then
info "使用 docker run 启动..."
docker stop go2rtc 2>/dev/null || true
docker rm go2rtc 2>/dev/null || true
docker run -d \
--name go2rtc \
--restart unless-stopped \
--network host \
-e TZ=Asia/Shanghai \
-v "$WORKDIR/go2rtc.yaml:/config/go2rtc.yaml" \
alexxit/go2rtc
info "go2rtc 已通过 docker run 启动"
fi
# 状态检查
sleep 2
echo ""
info "容器状态:"
docker ps --filter "name=go2rtc"
echo ""
info "访问地址:http://<服务器IP>:1984"
info "Web 界面默认端口 1984,RTSP 默认 8554"
info "如需查看日志:docker logs go2rtc"运行方法:
chmod +x install_go2rtc.sh
./install_go2rtc.sh注意:脚本会将 APT 源替换为清华镜像,并配置 Docker 镜像加速。若您有其他第三方 APT 源(如
tailscale),建议提前备份或删除相关.list文件以避免干扰。
4. 配置摄像头流
安装完成后,编辑 ~/go2rtc/go2rtc.yaml 文件,填入您的实际摄像头流信息。以下是根据您提供的摄像头 IP 和认证信息生成的完整配置:
streams:
camera1: rtsp://admin:123456@192.168.3.65:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera2: rtsp://admin:123456@192.168.3.64:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera3: rtsp://admin:123456@192.168.3.98:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera4: rtsp://admin:123456@192.168.3.64:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera5: rtsp://admin:123456@192.168.3.66:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera6: rtsp://admin:123456@192.168.3.87:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera7: rtsp://admin:123456@192.168.3.91:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera8: rtsp://admin:123456@192.168.3.92:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera9: rtsp://admin:123456@192.168.3.86:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera10: rtsp://admin:123456@192.168.3.82:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera11: rtsp://admin:123456@192.168.3.83:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera12: rtsp://admin:123456@192.168.3.67:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera13: rtsp://admin:123456@192.168.3.69:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera14: rtsp://admin:123456@192.168.3.68:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera15: rtsp://admin:123456@192.168.3.88:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera16: rtsp://admin:123456@192.168.3.70:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera17: rtsp://admin:123456@192.168.3.71:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera18: rtsp://admin:123456@192.168.3.89:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera19: rtsp://admin:123456@192.168.3.85:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera20: rtsp://admin:123456@192.168.3.73:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera21: rtsp://admin:123456@192.168.3.72:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera22: rtsp://admin:123456@192.168.3.84:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera23: rtsp://admin:123456@192.168.3.75:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera24: rtsp://admin:123456@192.168.3.80:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera25: rtsp://admin:123456@192.168.3.76:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera26: rtsp://admin:123456@192.168.3.74:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera27: rtsp://admin:123456@192.168.3.78:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera28: rtsp://admin:123456@192.168.3.79:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera29: rtsp://admin:123456@192.168.3.45:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera30: rtsp://admin:123456@192.168.3.54:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera31: rtsp://admin:123456@192.168.3.55:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera32: rtsp://admin:123456@192.168.3.56:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera33: rtsp://admin:123456@192.168.3.93:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera34: rtsp://admin:123456@192.168.3.94:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1
camera35: rtsp://admin:aaaa1111@192.168.3.95:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1替换配置文件后,重启容器使配置生效:
docker restart go2rtc关于重复 IP 的说明
配置中 camera2 和 camera4 都指向 192.168.3.64,若为同一台摄像头,请删除重复项;若是多路流,可保持不同名称,go2rtc 会分别建立连接。
5. 服务管理与使用
5.1 访问 Web 界面
部署成功后,在浏览器中打开:
http://<服务器IP>:1984可查看所有摄像头的实时画面、流地址及 WebRTC 链接。
5.2 常用 Docker 命令
# 查看运行状态
docker ps --filter "name=go2rtc"
# 查看实时日志
docker logs -f go2rtc
# 重启服务(修改配置后执行)
docker restart go2rtc
# 停止服务
docker stop go2rtc
# 删除容器和镜像
docker rm -f go2rtc
docker rmi alexxit/go2rtc5.3 修改配置
nano ~/go2rtc/go2rtc.yaml # 编辑流列表或添加其他选项
docker restart go2rtc # 重新加载配置6. 性能优化建议
网络模式:已使用
host网络,避免 NAT 转发开销。摄像头数量:35 路 RTSP 流对 CPU 有一定压力,如需长期稳定运行,可添加环境变量限制重连接数(参考 go2rtc 文档)。
日志清理:正常情况下 go2rtc 不会产生大量日志,但建议定期执行
docker logs --tail 100 go2rtc检查异常。安全防护:若暴露在公网,请为 Web 界面添加认证(在配置中设置
api或webrtc选项),或使用防火墙限制访问源 IP。
7. 常见问题
Q1:执行脚本时出现 Could not handshake 或 no Release file
由于系统中残留了 docker.com 的 APT 源,需删除:
sudo rm -f /etc/apt/sources.list.d/docker*.list
sudo apt updateQ2:docker-compose 下载失败
脚本会自动尝试使用 ghproxy.com 代理下载,若仍失败可手动下载二进制并放入 /usr/local/bin/docker-compose,或直接使用 docker run 方式(脚本已包含回退逻辑)。
Q3:摄像头显示离线
检查摄像头 IP 与本机是否在同一网段,能否 ping 通。
确认 RTSP 地址格式正确,可在电脑上用 VLC 测试。
查看 go2rtc 日志:
docker logs go2rtc | grep camera
附录 A:完整安装脚本
(已包含在第三章,此处不再重复。)
附录 B:后续升级 go2rtc 版本
docker pull alexxit/go2rtc
docker restart go2rtc配置文件和数据均持久化在宿主机目录,升级不会丢失。
通过以上步骤,您的边缘服务器即可稳定流媒体服务,轻松接入 35 路海康威视(或兼容 RTSP)摄像头。如有其他问题,欢迎继续交流。
访问地址
http://100.64.0.4:1984/stream.html?src=camera29&src=camera30&src=camera31&src=camera32&src=camera33&src=camera34&mode=webrtc,mse,hls,mjpeg