直播 obs

来源:互联网 发布:正交矩阵的性质 编辑:程序博客网 时间:2024/05/02 07:50

接下来我就简单跟大家介绍一下利用nginx来搭建流媒体服务器。

我选择的是腾讯云服务器

1、下载nginx-rtmp-module:

nginx-rtmp-module的官方github地址:https://github.com/arut/nginx-rtmp-module

使用命令

[php] view plain copy
  1. git clone https://github.com/arut/nginx-rtmp-module.git    
将nginx-rtmp-module下载到linux中。

2、安装nginx:

nginx的官方网站为:http://nginx.org/en/download.html

[php] view plain copy
  1. wget http://nginx.org/download/nginx-1.8.1.tar.gz    
  2. tar -zxvf nginx-1.8.1.tar.gz    
  3. cd nginx-1.8.1    
  4. ./configure --prefix=/usr/local/nginx  --add-module=../nginx-rtmp-module  --with-http_ssl_module      
这时有的人可能会报 没有configure 文件

那么就执行

[php] view plain copy
  1. yum -y install gcc gcc-c++ autoconf automake make  
完事之后 重新运行编码,之后执行
[php] view plain copy
  1. make && make install    
有的人可能会出现以下结果,保持淡定  我来帮你解决

当出现这个结果之后,大家来执行

[php] view plain copy
  1. yum -y install openssl openssl-devel  
或者
[php] view plain copy
  1. yum install -y openssl*  
然后大胆的往下执行  若是不行 就回滚回去 重新进行编译

本次默认安装目录为:/root, add-module为下载的nginx-rtmp-module文件路径。

安装时候可能会报错没有安装openssl

3、修改nginx配置文件:

[php] view plain copy
  1. vi /usr/local/nginx/conf/nginx.conf   
加入以下内容:

[html] view plain copy
  1. rtmp {  
  2.     server {  
  3.         listen 1935;  #监听的端口  
  4.         chunk_size 4000;     
  5.         application hls {  #rtmp推流请求路径    
  6.             live on;      
  7.             hls on;      
  8.             hls_path /usr/share/nginx/html/hls;      
  9.             hls_fragment 5s;      
  10.         }      
  11.     }      
  12. }    
hls_path需要可读可写的权限。
修改http中的server模块:
[php] view plain copy
  1. server {    
  2.     listen       81;    
  3.     server_name  localhost;    
  4.     
  5.     #charset koi8-r;    
  6.     
  7.     #access_log  logs/host.access.log  main;    
  8.     
  9.     location / {    
  10.         root   /usr/share/nginx/html;    
  11.         index  index.html index.htm;    
  12.     }    
  13.     
  14.     #error_page  404              /404.html;    
  15.     
  16.     # redirect server error pages to the static page /50x.html    
  17.     #    
  18.     error_page   500 502 503 504  /50x.html;    
  19.     location = /50x.html {    
  20.         root   html;    
  21.     }    

by the way ,root后路径可以跟据自己的需求来改的。
然后启动nginx:
[php] view plain copy
  1. /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf      

4、开始推流

做好以上的配置后,就可以开始推流了,我们可以使用obs来推流。

点击设定 来配置自己的服务器ip

URL为 rtmp://xxx:1935/hls,xxx为你的服务器的IP地址,hls是用来存放流媒体的。

秘钥可以随便填写一个,用来播放的时候识别播放哪个流媒体的,例如填写test等。

填写完毕后,点击开始串流,就说明我们的流媒体服务器搭建成功

5、观看直播(拉流)

观看直播就比较简单了,可以简单的使用h5的vedio标签就可以观看了。

可以访问http://xxx:81/hls/mystream.m3u8来观看直播,其中xxx为你的服务器IP地址,

或者使用

[php] view plain copy
  1. <video>      
  2.     <source src="http://xxx:81/hls/test.m3u8"/>      
  3.     <p class="warning">Your browser does not support HTML5 video.</p>      
  4. </video>    

同上, xxx写的是你服务器IP地址。

然后使用手机访问这个网站就能够观看直播了。延迟大概在20S左右。

(在iOS的safari浏览器中可以正常观看)

写在最后

为什么延迟 那么高呢?这是因为服务器将视频流切断成一个个小的以.ts结尾的文件。

而我们访问的是.m3u8文件,这个文件内容是将一个个ts文件串联起来的,这就达到了一个播放的效果,所以看起来会有很大的延迟

如果降低延迟也不是没有方法,可以设置切片生成的大小以及访问的速度,但是这样大大增加了服务器的压力。

当然,我们也可以用rtmp拉流工具(VLC等)来看该直播,延迟大概在2-5S左右,拉流地址与推流地址一致。

好了 至此简单的视频直播就完成了  如果大家有什么疑问可以在此评论交流 ,我们一起共同进步学习 !!
0 0
原创粉丝点击