首先编译安装nginx
[root@localhost ~]# tar xvf nginx-1.8.1.tar.gz
[root@localhost ~]# unzip nginx-rtmp-module-master.zip
[root@localhost ~]# cd nginx-1.8.1
[root@localhost ~]# yum -y install zlib zlib-devel openssl* pcre pcre-devel gd-devel #nginx的依赖包
[root@localhost nginx-1.8.1]# ./configure --add-module=../nginx-rtmp-module-master
[root@localhost nginx-1.8.1]# make -j 4 && make install
测试是否安装成功
[root@localhost nginx-1.8.1]# cd /usr/local/nginx
[root@localhost nginx ]# ./sbin/nginx #启动服务
在浏览器中输入虚拟机的IP或者localhost出现如下则说明编译安装成功
直播服务的配置
[root@centos6(cyn) nginx ]# vim /usr/local/nginx/conf/nginx.conf#在配置文件最后加上这段rtmp的配置
rtmp { #rtmp配置的块,这个地方就是直播的配置的根
server { #服务器的实例
listen 1935;#监听并且接受的端口
application mytv { #应用/实例的三种类型(1)rtmp录播(vod),(2)rtmp直播(live),(3)hls直播(hls)
live on; #开启直播直播模式,一对多广播
hls on; #开启直播
hls_path /usr/local/nginx/html/hls;#录制视频文件的目录/路径
hls_fragment 2s;#后面接时间,用来设置每一个块的大小。默认是5秒。只能为整数
}
}
}
下面是一些hls的配置,可根据自己的需求来配置
hls_playlist_length: 设置播放列表的长度,单位是秒
hls_sync: 音视频的同步时间
hls_continuous: on|off 设置连续模式,是从停止播放的点开始还是直接跳过
hls_nested: on|off 默认是off。打开后的作用是每条流自己有一个文件夹
hls_base_url: 设置基准URL,对于m3u8中使用相对URL有效
hls_cleanup: on|off 默认是开着的,是否删除列表中已经没有的媒体块
hls_fragment_naming: sequential(使用增长式的整数命名) | timestamp(使用媒体块的时间戳) | system(使用系统时间戳)命名方式
hls_fragment_naming_granularity: 如果使用时间戳命名时时间戳的精度
hls_fragment_slicing: plain(媒体块达到需要的duration就换)|aligned(当到达的数据库块时几个duration)
创建视频文件的目录/路径
[root@localhost nginx]# mkdir /usr/local/nginx/html/hls
重新启动服务,关闭防火墙及selinux
[root@localhost nginx]#./sbin/nginx -s stop
[root@localhost nginx]#./sbin/nginx
[root@localhost nginx]# iptables -F
[root@localhost nginx]# setenforce 0
到这里服务器已经搭建完毕了,我们开始推流设置
点击文件->设置->流,在流类型里选择自定义流媒体服务器,在URL中填入rtmp://172.18.252.106/mytv(IP加实例名称),然后在流名称中填入一个名字(任意),obs设置已经完成
点击开始推流,在左下角的来源中可以选择你要直播的内容
接下来去vlc media studio中设置接受流媒体
点击媒体->打开网络串流->网络,在URL中输入rtmp://172.18.252.106/mytv/test(服务器IP+应用名称+流名称),点击播放即可
至此一个obs推流-nginx-rtmp的hls直播-vlc串流的直播平台搭建完成,这个时候在浏览器中输入IP+stat
会显示我们的应用名称(mytv),live streames (流媒体的个数),流名称(test)