| 協議 | 延遲 | 適用場景 | 成熟度 |
|---|---|---|---|
| RTMP | 1-3 秒 | 直播、互動教學 | 極高(Nginx/SRS/Simple-RTMP-Server) |
| RTSP | < 1 秒 | 視訊會議、監控 | 中(Live555 需改源碼支援推流) |
| HLS | 10-30 秒 | 點播、大規模分發 | 高(原生 HTTP,CDN 友好) |
| WebRTC | < 500ms | 實時通話、遠程桌面 | 高(複雜度高,需信令服務器) |
結論:直播首選 RTMP,Nginx-rtmp-module 部署最簡單、效能最強。
┌─────────┐ RTMP 推流 ┌──────────────┐ RTMP/HTTP-FLV/HLS 拉流 ┌─────────┐
│ OBS │ ─────────────────▶ │ Nginx-RTMP │ ──────────────────────────────▶ │ 觀衆端 │
│ 推流端 │ rtmp://ip/live/ │ 推流/轉碼/ │ flv.js / VLC / SRS Player │ 拉流端 │
└─────────┘ stream_key │ 錄製/轉封裝 │ └─────────┘
└──────────────┘
# 安裝依賴
sudo apt update && sudo apt install -y build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev git
# 下載源碼
wget https://nginx.org/download/nginx-1.24.0.tar.gz
git clone https://github.com/arut/nginx-rtmp-module.git
# 編譯安裝
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module
make && sudo make install
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on; # 開啟直播
record off; # 關閉錄製(或按需開啟)
# 錄製範例:
# record all;
# record_path /data/record;
# record_unique on;
# 轉碼範例(需 ffmpeg):
# exec ffmpeg -i rtmp://localhost/live/$name
# -c:v libx264 -preset veryfast -tune zerolatency -c:a aac
# -f flv rtmp://localhost/hls/$name;
}
application hls {
live on;
hls on; # 啟用 HLS
hls_path /data/hls;
hls_fragment 3s;
hls_playlist_length 60s;
}
}
}
http {
server {
listen 80;
location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; }
location /stat.xsl { root /usr/local/nginx/html; }
location /hls { types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } add_header Cache-Control no-cache; }
}
}
sudo /usr/local/nginx/sbin/nginx
# 檢查端口
netstat -ntlp | grep -E '80|1935'
# 查看統計頁
curl https://localhost/stat
sudo add-apt-repository ppa:obsproject/obs-studio && sudo apt install obs-studio)rtmp://你的服務器IP/live → 流密鑰:test123打開 SRS Player → URL 填入 https://你的服務器IP/live/test123.flv → 播放
媒體 → 打開網絡串流 → 輸入 rtmp://你的服務器IP/live/test123 → 播放
ffplay -fflags nobuffer -flags low_delay -framedrop -strict experimental rtmp://IP/live/test123
rtmps、https-flvon_publish 回調驗證 stream_key 合法性live_hd、live_sd,播放端自適應切換/stat 指標(連接數、帶寬、幀率)+ Grafana 儀表板