項目需求, 使用 rtp 內網組播推流, 內網需要有一個台機器對 流做存儲,轉換成 mp4
方案1: 媒體服務器方案, 使用媒體服務器, 可以做到高並發, 協議相互轉換, 流存儲, 內網直播轉外網
方案2: 直接使用 ffmpeg, 按照需求 將直播流轉換成 mp4存儲,
本項目比較簡單,只需要存儲, 采用第二個方案 https://www.cnblogs.com/han-guang-xue/p/16056041.html
該文檔主要針對方案一
原文檔(定期更新): https://gitee.com/han_gx/my_notes/blob/master/nginxRtmp/rtsp-媒體服務器搭建.md
rtsp 媒體服務器選型
1.rtsp-server
主要針對 RTSP協議的輕量級服務器, 由四人組成. 從github
中 issues
shell https://github.com/revmischa/rtsp-server
2. ZLMediaKit
https://github.com/ZLMediaKit/ZLMediaKit
https://gitee.com/ZLMediaKit/ZLMediaKit
經過商用長時間驗證,性能可靠穩定, 目前由58位多開發人員提供
- 支持多種協議(RTP/RTSP/RTMP/HLS/HTTP-FLV/WebSocket-FLV/GB28181/HTTP-TS/WebSocket-TS/HTTP-fMP4/WebSocket-fMP4/MP4/WebRTC),支持協議互轉。
- 使用多路復用/多線程/異步網絡IO模式開發,並發性能優越,支持海量客戶端連接。
- 支持linux、macos、ios、android、windows全平台。
- 支持畫面秒開、極低延時(500毫秒內,最低可達100毫秒)。
- 提供完善的標准C API,可以作SDK用,或供其他語言調用
- 同時支持 rtsp 和 rtmp
- 支持 docker 容器
...
優點甚多,官網介紹很詳細
rtsp 性能測試
https://github.com/ZLMediaKit/ZLMediaKit/wiki/RTSP性能優化
3. 環境搭建(ZLMediaKit)
基於 docker 安裝
docker安裝`https://docs.docker.com/engine/install/centos/#installation-methods`
1. 安裝 docker 引擎
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum-config-manager --enable docker-ce-nightly
# 安裝 最新的docker版本
yum install docker-ce docker-ce-cli containerd.io
# 列舉 docker 版本, 如果需要安裝其他版本的docker, 可以使用下面的命令
yum list docker-ce --showduplicates | sort -r
# 安裝特定版本的 docker
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
# 啟動 docker
systemctl status/stop/restart/disable/enable docker
# 開機自啟動
sudo systemctl enable/disable docker.service
sudo systemctl enable/disable containerd.service
# docker 相關操作
docker ps -a # 查看所有容器
docker run hello-world # 執行 hello-world 容器
docker search xxxxx # 查找在線容器
docker pull xxxx:version # 下載容器
docker images # 查看所有鏡像
2. 安裝 zlmediakit 鏡像並運行, 該鏡像由官方提供
docker pull zlmediakit/zlmediakit:Release.last
# -p 本地端口:遠程端口
docker run -id -p 1935:1935 -p 8081:80 -p 8554:554 -p 10000:10000 -p 10000:10000/udp -p 8000:8000/udp zlmediakit/zlmediakit:Release.last
3. 推流拉流
# rtsp tcp 推流, test1.webm 是一個 8K 測試視頻,也可以使用其他的
ffmpeg -re -i "./test1.webm" -vcodec h264 -acodec aac -f rtsp -rtsp_transport udp rtsp://101.43.179.35:8554/live/test
# ffplay 拉流
ffplay http://101.43.179.35:8081/live/test/hls.m3u8
# rtp 推流
ffmpeg -re -i "./test1.webm" -vcodec h265 -acodec aac -f rtp_mpegts rtp://101.43.179.35:10000
可以輸入web服務地址查看上傳的流文件, 然后通過 ffplay 播放即可
4.基於docker容器如何修改配置文件
目前我並沒有發現在容器中如何修改相關配置文件, 不過官方提供了API接口, 通過API配置服務器相關參數, 通過 getServerconfig 和 setServerconfig, 查看和配置服務器, 通過設置參數 on_record_mp4,用來讓媒體服務器記錄推流為 mp4 格式, 保存視頻以固定時長,官方文檔並沒有提及到,源碼安裝也同樣沒提交到相關配置項,不過通過 ffmpeg可以實現大的視頻拆分
ffmpeg -ss 00:00:00 -i input.mp4 -c copy -t 1200 output.mp4
服務API接口: https://github.com/zlmediakit/ZLMediaKit/wiki/MediaServer支持的HTTP-API#3indexapigetserverconfig
基於 源碼搭建服務
官方文檔 https://github.com/ZLMediaKit/ZLMediaKit/wiki/快速開始
環境 Centos8 系統
安裝 額外依賴
yum install -y gcc gcc-c++ cmake
安裝 ffmpeg, 並且要下載配置使其支持 h264 視頻編碼
wget https://www.nasm.us/pub/nasm/releasebuilds/2.14/nasm-2.14.tar.gz ## 安裝 x264 需要 nasm
cd nasm-2.14 && ./configure && make && make install
git clone https://code.videolan.org/videolan/x264.git
cd x264 && ./configure --prefix=/usr/x264/ --includedir=/usr/local/include --libdir=/usr/local/lib --enable-shared && make && make install
curl -O http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz ## 下載依賴
tar -zxvf yasm-1.3.0.tar.gz && cd yasm-1.3.0 && ./configure && make && make install
git clone https://gitee.com/mirrors/ffmpeg
cd ffmpeg && ./configure && make
編譯
cd ZLMediaKit
mkdir build
cd build
#macOS下可能需要這樣指定openss路徑:cmake .. -DOPENSSL_ROOT_DIR=/usr/local/Cellar/openssl/1.0.2j/
cmake ..
make -j4
運行
cd ZLMediaKit/release/linux/Debug # 該目錄下有各種運行命令,可以拷貝到其他的地方
./MediaServer ## 運行項目
./MediaServer -h ## 查看配置文件
config.ini ## 配置文件,詳細配置請查https://github.com/zlmediakit/ZLMediaKit/blob/master/conf/config.ini
推流記錄mp4文件
修改配置文件 config.ini
#是否默認推流時mp4錄像,hook接口(on_publish)中可以覆蓋該設置
publishToMP4=0