搭建 RTMP协议的 直播服务平台
基于Nginx的RTMP协议推流直播平台,尽管我早在2017年就位上海的雇主搭建并使用过,而且在2018年为河南移动服务期间也再次搭建过,但把这个平台的搭建部署作为工作经历讲给河南的雇主听时,总被怀疑项目实施的真实性,甚至因为不提供给他们项目配置文档而被喷为项目简历造假,原因很简单:这些小公司需要这项服务、但仅凭他们自己玩儿不转,还不想付钱购买相关商业技术,无处下手时就想以招聘工程师的噱头来骗取相关解决方案。
现在我把主要配置文件的模板放出来,一是要打河南骗子公司的脸,二是我打算离开郑州的行当了。以我对这些诈骗公司的了解,即便我白送他们一套开源直播平台,它们一样玩儿不转。眼界决定了事业发展的高度和广度;对人是否尊重决定了一家公司能否长久运营。一门心思地想骗取现实操作中的解决方案、恶意拖欠工资,勾结部分当权的败类欺压外地人,终将无路可走!
平台业务逻辑描述:
本平台所需软件组:
- 视频录制:Open Broadcaster Software
- Nginx推流:CentOS 7 1804、Nginx 1.16.1、nginx-rtmp-module 1.2.1、ffmpeg 4.2.1
- Nginx推流的功能实现:本机分流直播、直播云平台分流直播
平台部署步骤:
- 初始化CentOS 7 系统,可参考本人的OS初始化脚本;
- 编译安装 Nginx 1.16.1、nginx-rtmp-module 1.2.1、ffmpeg 4.2.1 ,并设定为开机自启动服务,软件安装路径统一为 /opt/RTMPserver;
- 下载SSL证书并安装到Nginx推流服务器,一个可行的安装方式可参考北桥苏在腾讯云上的操作,记住要安装Nginx的证书(https://www.cnblogs.com/zerofc/p/9974564.html)。安装后修改Nginx的配置文件:
cat /etc/nginx/nginx.conf
listen 443 ssl;
ssl_certificate /opt/certs/example.com.crt;
ssl_certificate_key /opt/certs/example.com.key;
(4)RTMP推流服务的配置
cat nginx-rtmp-ffmpeg-conf/nginx.conf
daemon off;
error_log /dev/stdout info;
events {
worker_connections 1024;
}
rtmp {
server {
#RTMP监听端口
listen 1935;
#流整合块,默认4096,值越大CPU负载就越小
chunk_size 4096;
#RTMP 直播流配置
application stream {
live on;
#添加水印及分流,测试时直接分流到当前服务器hls,实际使用一般都分流到直播云平台,替换分流地址即可。视频参数为:1920x1080分辨率,aac音轨,30画面,FLV制式
exec ffmpeg -i rtmp://localhost:1935/stream/流名
#添加水印:rtmp://localhost:1935/hls/流名_wm
-map '[流名]' -map 0:a -s 1920x1080 -c:v libx264 -c:a aac -g 30 -r 30 -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost:1935/hls/流名_wm
#不用水印:rtmp://localhost:1935/hls/流名
-c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 2500k -f flv -g 30 -r 30 -s 1920x1080 -preset superfast -profile:v baseline rtmp://localhost:1935/hls/流名;
#分流至腾讯云直播
#exec ffmpeg -i rtmp://localhost:1935/stream/流名
#添加水印:rtmp://localhost:1935/hls/流名_wm
#-map '[流名]' -map 0:a -s 1920x1080 -c:v libx264 -c:a aac -g 30 -r 30 -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://live-push.tencent.com/stream/流名_wm
#不用水印:rtmp://localhost:1935/hls/流名
# -c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 2500k -f flv -g 30 -r 30 -s 1920x1080 -preset superfast -profile:v baseline rtmp://live-push.tencent.com/stream/流名;
#分流至阿里云
#exec ffmpeg -i rtmp://localhost:1935/stream/流名
#添加水印:rtmp://localhost:1935/hls/流名_wm
#-map '[流名]' -map 0:a -s 1920x1080 -c:v libx264 -c:a aac -g 30 -r 30 -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://live-push.aliyun.com/stream/流名_wm
#不用水印:rtmp://localhost:1935/hls/流名
# -c:a libfdk_aac -b:a 128k -c:v libx264 -b:v 2500k -f flv -g 30 -r 30 -s 1920x1080 -preset superfast -profile:v baseline rtmp://live-push.aliyun.com/stream/流名;
}
application hls {
live on;
hls on;
hls_fragment 5;
hls_path /opt/broadcast/hls;
}
}
}
http {
access_log /dev/stdout combined;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
server {
listen 80;
# Uncomment these lines to enable SSL.
# Update the ssl paths with your own certificate and private key.
# listen 443 ssl;
# ssl_certificate /opt/certs/example.com.crt;
# ssl_certificate_key /opt/certs/example.com.key;
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /opt/broadcast;
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Origin *;
}
location /live {
alias /opt/broadcast/hls;
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Origin *;
}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet static/stat.xsl;
}
location /static {
alias /www/static;
}
location = /crossdomain.xml {
root /www/static;
default_type text/xml;
expires 24h;
}
}
}
更多关于ffmpeg的选项设置说明请参见
https://ffmpeg.org/ffmpeg.html
HLS播放地址:http://推流服务器IP:8080/hls/流名.m3u8
RTMP播放地址:rtmp://推流服务器IP:1935/stream/流名
(5)OBS推流配置
打开Open Broadcaster Software的“设置”-“推流”窗口,依次填入如下内容:
服务: Custom Streaming Server
服务器: rtmp://<server ip>:1935/stream
流秘钥:自定义,建议与流名相同