1. 安装
a. 环境选择,linux
[root@200 software]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@200 software]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[root@200 nginx-1.16]# ifconfig
eth0: flags=4163
inet 192.168.1.205 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::9435:12c3:18b9:534e prefixlen 64 scopeid 0x20
b. 下载 nginx
登录官方网站 nginx.org 下载
下载stable版本,我们以 2019-08-13 nginx-1.16.1为例
[root@200 software]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
c. 下载 nginx-rmp 模块
登录官方网站 https://github.com/arut/nginx-rtmp-module/releases 下载
下载release版本,我们以 1.2.1 nginx-1.16.1为例
[root@200 software]# wget https://codeload.github.com/arut/nginx-rtmp-module/tar.gz/v1.2.1 -o nginx-rtmp-module-1.2.1.tar.gz
d. 编译安装
[root@200 software]# tar -xvzf nginx-1.16.1.tar.gz
[root@200 nginx-1.16.1]# tar -xvzf nginx-rtmp-module-1.2.1.tar.gz
[root@200 nginx-1.16.1]# cd nginx-1.16.1
[root@200 nginx-1.16]# yum install pcre openssl
[root@200 nginx-1.16.1]#./configure --prefix=/usr/local/nginx-1.16 --add-module=../nginx-rtmp-module-1.2.1 --with-http_ssl_module
[root@200 nginx-1.16.1]# make -j 4 && make install
2. 配置
a. rtmp 配置
[root@200 nginx-1.16]# cd /usr/local/nginx-1.16/
[root@200 nginx-1.16]# cat conf/nginx.conf
修改 nginx 的配置,增加 rtmp 节点,注意是和 HTTP 平级,参考下面的内容
rtmp{
#RTMP服务
server{
#//服务端口
listen 1935;
#//数据传输块的大小
chunk_size 4096;
application vod {
#//视频文件存放位置
play /root/video;
}
#设置频道(房间),多路配置多个 application
application live {
#第一处添加的直播字段
live on;
#如果懒得推流,那可以用这个,香港卫视的直播推流
#pull rtmp://live.hkstv.hk.lxdns.com/live/hks;
record off;
}
application hls {
#支持苹果终端 HLS 协议
live on;
hls on;
hls_path /tmp/hls;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
此处省略....
b. http 直播流状态配置
需要增加 location /stat 和 location /stat.xsl
http{
此处省略....
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /usr/local/nginx-1.16/hls/;
}
[root@200 nginx-1.16]# cat conf/nginx.conf
[root@200 nginx-1.16]# mkdir /root/video
[root@200 nginx-1.16]# mkdir /usr/local/nginx-1.16/hls
[root@200 nginx-1.16]# cp /home/software/nginx-rtmp-module-1.2.1/stat.xsl /usr/local/nginx-1.16/hls
c. 服务启动
[root@200 nginx-1.16]# /usr/local/nginx-1.16/sbin/nginx
[root@200 nginx-1.16]# /usr/local/nginx-1.16/sbin/nginx -s reload
[root@200 nginx-1.16]# /usr/local/nginx-1.16/sbin/nginx -s reopen
3. 验证点播
a. 工具准备
windows 机器安装 VLC media player
windows 机器安装 OBS Studio
b. 拷贝 test1.mp4 到 linux 服务器 /root/video
[root@200 nginx-1.16]# ls /root/video/test1.mp4
/root/video/test1.mp4
c. 打开 VLC media player 点播
打开网络串流,播放网络流,输入地址
rtmp://192.168.1.205:1935/vod/test1.mp4
4. 验证直播
a. 工具准备(已经安装可忽略)
windows 机器安装 VLC media player
windows 机器安装 OBS Studio
b. 准备推直播(最好带摄像头)
打开 OBS Studio,设置 推流地址为
rtmp://192.168.1.205:1935/live
选择 直播流 数据源为 视频捕获设备(摄像头),从摄像头获取视频流
点击推送,开始推流
如果没有摄像头,也可以考虑使用 FFmpeg推流(此功能未验证)
ffmpeg -re -i /root/video/test1.mp4 -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://192.168.1.205:1935/live/
ffmpeg -re -i /root/video/test1.mp4 -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://192.168.1.205:1935/hls/
c. 查看状态
http://192.168.1.205/stat
RTMP #clients Video Audio In bytes Out bytes In bits/s Out bits/s State Time
Accepted: 21 codec bits/s size fps codec bits/s freq chan 307.85 MB 277.48 MB 2.83 Mb/s 1.79 Mb/s 15h 36m 14s
vod
vod streams 0
live
live streams 2
[EMPTY] 2 H264 High 3.1 2.66 Mb/s 1280x720 30 AAC LC 149 Kb/s 44100 2 27.4 MB 21.69 MB 2.81 Mb/s 1.38 Mb/s active 1m 27s
Generated by nginx-rtmp-module 1.1.4, nginx 1.16.1, pid 7480, built Dec 12 2019 17:36:44 gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
d. 打开 VLC media player 查看直播流
打开网络串流,播放网络流,输入地址
rtmp://192.168.1.205:1935/live
浏览器端可以直接加载 video.js 播放,也可以考虑加载 VLC media player plugin 播放。