前言
在实际项目中,我们经常需要将本地视频文件转换为 RTSP 流进行测试或演示。本文将介绍如何使用 Docker 和 MediaMTX 快速搭建一个支持密码认证的 RTSP 视频流服务器,并能够循环播放 MP4 文件。
技术栈
- Docker & Docker Compose: 容器化部署
- MediaMTX: 开源的流媒体服务器(原名 rtsp-simple-server)
- FFmpeg: 视频处理工具
项目结构
1 2 3 4 5 6
| rtsp-server/ ├── docker-compose.yml ├── mediamtx.yml └── videos/ ├── video1.mp4 └── video2.mp4
|
配置文件
1. docker-compose.yml
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 27 28
| version: '3.8'
services: mediamtx: image: bluenviron/mediamtx:latest-ffmpeg container_name: rtsp-server ports: - "8554:8554" - "1935:1935" - "8888:8888" - "8889:8889" - "9997:9997" volumes: - ./mediamtx.yml:/mediamtx.yml - ./videos:/media restart: unless-stopped networks: - rtsp-network healthcheck: test: ["CMD", "wget", "--spider", "-q", "http://localhost:9997/v3/config/global/get"] interval: 30s timeout: 10s retries: 3
networks: rtsp-network: driver: bridge
|
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
logLevel: info
logDestinations: [stdout]
rtspAddress: :8554 rtspTransports: [tcp, udp]
authMethod: internal authInternalUsers: - user: admin pass: password123 permissions: - action: read path: "~^.*$" - user: publisher pass: publish123 permissions: - action: publish path: "~^.*$"
paths: stream1: source: publisher runOnInit: > ffmpeg -re -stream_loop -1 -i /media/video1.mp4 -c copy -f rtsp -rtsp_transport tcp rtsp://publisher:publish123@localhost:$RTSP_PORT/$MTX_PATH runOnInitRestart: yes
stream2: source: publisher runOnInit: > ffmpeg -re -stream_loop -1 -i /media/video2.mp4 -c copy -f rtsp -rtsp_transport tcp rtsp://publisher:publish123@localhost:$RTSP_PORT/$MTX_PATH runOnInitRestart: yes
hlsAddress: :8888 hlsAlwaysRemux: yes hlsVariant: lowLatency
webrtcAddress: :8889
api: yes apiAddress: :9997
|
部署步骤
1. 创建项目目录
1 2
| mkdir -p rtsp-server/videos cd rtsp-server
|
2. 创建配置文件
将上面的 docker-compose.yml 和 mediamtx.yml 保存到项目目录。
3. 准备视频文件
1 2
| cp /path/to/your/video.mp4 videos/video1.mp4
|
4. 启动服务
1 2 3 4 5
| docker-compose up -d
docker-compose logs -f
|
你应该看到类似这样的日志输出:
1 2 3 4 5 6 7
| INF [path stream1] runOnInit command started ffmpeg version ... Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/media/video1.mp4': Duration: 00:01:30.00, start: 0.000000, bitrate: 1500 kb/s Stream mapping: Stream #0:0 -> #0:0 (copy) Stream #0:1 -> #0:1 (copy)
|
使用方法
1. VLC 播放器
打开 VLC → 媒体 → 打开网络串流,输入:
1
| rtsp://admin:password123@your_server_ip:8554/stream1
|
2. FFplay 命令行
1
| ffplay -rtsp_transport tcp rtsp://admin:password123@localhost:8554/stream1
|
3. Python (OpenCV)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import cv2
url = "rtsp://admin:password123@localhost:8554/stream1" cap = cv2.VideoCapture(url)
while True: ret, frame = cap.read() if not ret: break cv2.imshow('RTSP Stream', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break
cap.release() cv2.destroyAllWindows()
|
4. HLS 网页播放
可以通过 HLS 在网页中播放:
1
| http://your_server_ip:8888/stream1/index.m3u8
|
常用命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| docker-compose ps
docker-compose logs -f mediamtx
docker-compose restart
docker-compose down
docker-compose exec mediamtx sh
curl http://localhost:9997/v3/paths/list
|
配置说明
认证系统
MediaMTX 使用 authInternalUsers 配置用户权限:
- read: 观看流的权限
- publish: 推流的权限
- playback: 回放权限
- api: API访问权限
多用户权限配置示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| authInternalUsers: - user: admin pass: admin123 permissions: - action: read path: "~^.*$" - user: user1 pass: user123 permissions: - action: read path: "~^stream1$" - user: user2 pass: user456 permissions: - action: read path: "~^stream2$"
|
runOnInit vs runOnDemand
- runOnInit: 服务启动时就开始推流,持续运行
- runOnDemand: 有客户端连接时才启动推流,节省资源
本文使用 runOnInit 确保流立即可用。
测试工具
1. 快速连接测试
1 2 3 4 5 6 7 8
| curl -v rtsp://admin:password123@localhost:8554/stream1
telnet localhost 8554
nc -zv localhost 8554
|
2. 流信息检查
1 2 3 4 5 6
| ffprobe -rtsp_transport tcp rtsp://admin:password123@localhost:8554/stream1
curl http://localhost:9997/v3/paths/list curl http://localhost:9997/v3/paths/get/stream1
|
3. 诊断脚本
创建 diagnose.sh:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #!/bin/bash echo "🔍 RTSP服务诊断" echo "===================="
echo -e "\n1️⃣ 容器状态" docker-compose ps
echo -e "\n2️⃣ 端口监听" netstat -tlnp | grep 8554
echo -e "\n3️⃣ 视频文件" ls -lh videos/
echo -e "\n4️⃣ API状态" curl -s http://localhost:9997/v3/paths/list
echo -e "\n5️⃣ 最近日志" docker-compose logs --tail=20 mediamtx
|
常见问题
1. FFmpeg 未找到
错误信息:
1
| exec: "ffmpeg": executable file not found in $PATH
|
解决方案:
确保使用 bluenviron/mediamtx:latest-ffmpeg 镜像,而不是 bluenviron/mediamtx:latest。
2. VLC 无法打开流
可能原因:
- 用户名密码错误
- 端口未开放
- FFmpeg 未正常启动
- 视频文件路径错误
排查步骤:
1 2 3 4 5 6 7 8
| docker-compose logs mediamtx | grep -i error
curl http://localhost:9997/v3/paths/list
docker-compose exec mediamtx ls -la /media/
|
3. 流延迟较高
优化配置:
1 2 3 4 5 6 7 8 9 10 11
| paths: stream1: runOnInit: > ffmpeg -re -stream_loop -1 -i /media/video1.mp4 -preset ultrafast -tune zerolatency -c copy -f rtsp -rtsp_transport tcp rtsp://publisher:publish123@localhost:$RTSP_PORT/$MTX_PATH
|
安全建议
- 修改默认密码: 使用强密码,避免使用示例中的密码
- 限制访问: 通过防火墙限制访问 IP
- 使用环境变量: 敏感信息通过环境变量传递
- 定期更新: 保持 Docker 镜像更新
使用环境变量
创建 .env 文件:
1 2 3
| RTSP_USER=myuser RTSP_PASS=mySecurePassword123! PUBLISHER_PASS=publisherSecurePass456!
|
修改 mediamtx.yml 使用环境变量(需要配合脚本预处理)。
高级功能
1. 录制视频流
1 2 3
| ffmpeg -i rtsp://admin:password123@localhost:8554/stream1 \ -t 10 -c copy output.mp4
|
2. 转码推流
1 2 3 4
| ffmpeg -re -i input.mp4 \ -c:v libx264 -preset fast -c:a aac \ -f rtsp rtsp://publisher:publish123@localhost:8554/custom_stream
|
3. 多码率配置
可以配置多个不同码率的流,供不同网络环境使用。
性能优化
1. 资源限制
在 docker-compose.yml 中添加:
1 2 3 4 5 6 7 8 9 10 11
| services: mediamtx: deploy: resources: limits: cpus: '2' memory: 2G reservations: cpus: '1' memory: 512M
|
2. 日志轮转
1 2 3 4 5 6 7 8
| services: mediamtx: logging: driver: "json-file" options: max-size: "10m" max-file: "3"
|
总结
本文介绍了如何使用 Docker 和 MediaMTX 快速搭建一个功能完善的 RTSP 视频流服务器。该方案具有以下优势:
- ✅ 部署简单,一键启动
- ✅ 支持密码认证,安全可靠
- ✅ 支持多种协议(RTSP、HLS、WebRTC)
- ✅ 循环播放本地视频文件
- ✅ 配置灵活,易于扩展
适用于视频监控测试、流媒体演示、开发调试等多种场景。
参考资料