直播推流拉流技术。 2018-01-26
前段时间,做了个项目。参考网站有直播功能,但是我们那个项目不用做直播功能。好吧,那就不做直播功能了。但是看到对方居然能直播,要不咱得闲了研究一下?
说整就整。百度得知NGINX有个扩展,还是插件,又或者叫模块。反正就是需要额外安装一个叫rtmp的东西来配合上nginx。没错就这么简单。
工具,obs推流工具。服务器一台(我是centos7 虚拟机)。安装下nginx和nginx的扩展rtmp。。
接下来可能就是一段命令了。
如果是虚拟机自己获取下虚拟机ip 我的是虚拟机 ip地址 192.168.227.129
1、下载nginx-rtmp-module: nginx-rtmp-module的官方github地址:https://github.com/arut/nginx-rtmp-module
使用命令: git clone https://github.com/arut/nginx-rtmp-module.git
将nginx-rtmp-module下载到linux中。
2、安装nginx: nginx的官方网站为:http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar -zxvf nginx-1.8.1.tar.gz
cd nginx-1.8.1
./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-http_ssl_module
make && make install
本次默认安装目录为:/root, add-module为下载的nginx-rtmp-module文件路径。 安装时候可能会报错没有安装openssl,需要执行命令:
yum -y install openssl openssl-devel
3、修改nginx配置文件:
vi /usr/local/nginx/conf/nginx.conf
加入以下内容:
rtmp {
server {
listen 1935; #监听的端口
chunk_size 4000;
application hls { #rtmp推流请求路径
live on;
hls on;
hls_path /usr/share/nginx/html/hls;
hls_fragment 5s;
}
}
}
hls_path需要可读可写的权限。 修改http中的server模块:
server {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
当然了,root可以跟据自己的需求来改的。 然后启动nginx:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
4、开始推流 做好以上的配置后,就可以开始推流了,我们可以使用obs来推流。 IP替换成你的就成
我们设置下我们播放源
OK 选择完毕我们可以开始推流了。
如果失败会报错的。那我们如何拉流? 我们刚是不是搞了个81端口,那就是用户播放的流地址。 如果是你的系统是在服务器上的话 直接用手机访问 http://192.168.227.129:81/hls/mystream.m3u8
如果你的系统是虚拟机的话,建议使用video播放器来播放。请百度video标签播放 m3u8视频 hls流 等关键字
小小干货请笑纳。哈哈哈