nginx的第三方模块nginx-rtmp-module来实现rtmp的推流和hls的播放
我在这里使用的是192.168.1.151这台服务器
yum -y install gcc gcc-c++ autoconf automake make openssl openssl-devel pcre-devel git
git clone https://github.com/arut/nginx-rtmp-module.git
wget http://nginx.org/download/nginx-1.8.1.tar.gz
wget https://ftp.pcre.org/pub/pcre/pcre-8.35.tar.gz
tar -zxvf nginx-1.8.1.tar.gz
tar xf pcre-8.35.tar.gz
cd nginx-1.8.1
./configure --prefix=/usr/local/nginx --with-pcre=/root/pcre-8.35 --add-module=/root/nginx-rtmp-module --with-http_ssl_module
make && make install
修改配置文件:
nginx.conf
error_log logs/error.log debug;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on;
record off;
}
#application live2 {
#live on;
# record off;
# }
# video on demand
# application vod {
# play /var/flvs;
# }
# application vod_http {
# play http://192.168.31.185/vod;
# }
application hls {
live on;
hls on;
hls_path /tmp/hls; #ts切片的存放目录,可以自行创建
}
}
}
# HTTP can be used for accessing RTMP stats
http {
server {
listen 80;
# This URL provides RTMP statistics in XML
location /stat {
rtmp_stat all;
# Use this stylesheet to view XML as web page
# in browser
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
# XML stylesheet to view RTMP stats.
# Copy stat.xsl wherever you want
# and put the full directory path here
root /path/to/stat.xsl/; #stat.xsl文件也可以直接拷贝到nginx的默认文件中,也可以重新创建一个目录放入其中,但是一定在这里指定
}
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
location /dash {
# Serve DASH fragments
root /tmp;
add_header Cache-Control no-cache;
}
location / {
root /tmp/hls;
index index.html;
}
}
}
上传crossdomain.xml到nginx的默认访问路径中并给定权限
chown nobody.nobody /opt/hls/crossdomain.xml #应为在nginx的配置文件中没有定义用户和组,所以产生的直播流ts切片的用户和组也是nobody所以这里要和ts切片的用户组保持一致
测试:
先下载windows安装程序
VLC:http://rj.baidu.com/soft/detail/12124.html?ald
OBS: https://obsproject.com/download

点击开始推流:

此时在ts切片存放处会生成ts切片和一个m3u8文件
[root@localhost conf]# ll /tmp/hls/
total 5820
-rw-r--r--. 1 nobody nobody 259 Jan 25 17:59 crossdomain.xml
-rw-r--r--. 1 nobody nobody 2830904 Jan 26 10:36 yyy-0.ts
-rw-r--r--. 1 nobody nobody 2867752 Jan 26 10:36 yyy-1.ts
-rw-r--r--. 1 nobody nobody 244776 Jan 26 10:36 yyy-2.ts
-rw-r--r--. 1 nobody nobody 142 Jan 26 10:36 yyy.m3u8
这是就可以在VLC中进行测试:


此时如果出现图像并在上面显示以http协议进行播放

浙公网安备 33010602011771号