centos 7搭建直播间

前白本来这块需要介绍一些常用流协议的地址,百度上搜索出现一大堆,这里就不再赘述。

一、安装Nginx的软件依赖

yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel 

二、安装RTMP与nginx模块

[root@tecent ~]# cd /usr/local
[root@tecent ~]# groupadd nginx
[root@tecent ~]# useradd -s /sbin/nologin -g nginx -M nginx
[root@tecent ~]# yum install wget -y
[root@tecent ~]# wget -c  http://nginx.org/download/nginx-1.13.12.tar.gz
[root@tecent ~]# tar  -zxvf  nginx-1.13.12.tar.gz
[root@tecent ~]# mkdir  -p  /usr/local/nginx/module
[root@tecent ~]# mkdir  -p   /usr/local/nginx/myapp
[root@tecent ~]# mkdir -p /usr/local/nginx/vod/flvs
[root@tecent ~]# mkdir /var/log/nginx
[root@tecent ~]# cd   /usr/local/nginx/module
[root@tecent ~]# yum install git -y
[root@tecent ~]# git clone  https://github.com/arut/nginx-rtmp-module.git
[root@tecent ~]# cd  /usr/local/nginx-1.13.12/
[root@tecent ~]# ./configure --prefix=/usr/local/nginx --add-module=/usr/local/nginx/module/nginx-rtmp-module
[root@tecent ~]# make && make install
[root@tecent ~]# /usr/local/nginx/sbin/nginx  #到此步测试在浏览器上输入ip
地址会出现nginx的欢迎页面

三、配置nginx

1.vim /usr/local/nginx/conf/nginx.conf,底下的参数信息是配置好的,可以直接覆盖

user  nginx;
worker_processes  1;
error_log  logs/error.log debug;
events {
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;

        application myapp {
        live on;
	    hls on;
   	    hls_path  /usr/local/nginx/myapp;
	    wait_key on; 
	     #allow publish 127.0.0.1;
         #deny publish all;
        # rtmp推流请求路径,文件存放路径
 
        # 每个TS文件包含视频内容的长度
        hls_fragment 2s;
 
        # 总共可以回看的事件
        hls_playlist_length 60s;
 
        # 连续模式
        hls_continuous on;
 
        # 对多余的切片进行删除
        hls_cleanup on;
 
        # 嵌套模式
        hls_nested on;
        #record keyframes;
        #record_path /tmp;
        #record_max_size 128K;
        #record_interval 30s;
        #record_suffix .this.is.flv;
        #on_publish http://localhost:8080/publish;
        #on_play http://localhost:8080/play;
        #on_record_done http://localhost:8080/record_done;
        }
	
		application vod {
			play /usr/local/nginx/vod/flvs;
		}
    }
}

http {

	include       mime.types;
    default_type  application/octet-stream;
	server_tokens off;   #关闭服务器的版本号
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /var/log/nginx/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
    
    keepalive_timeout  65;
    
    #gzip  on;
    server {
        listen      80;
		# http播放地址
        location /live {
       	types {
				 application/vnd.apple.mpegurl m3u8;
				 video/mp2t ts;
              } 
           alias /usr/local/nginx/myapp;   
           expires -1;
           add_header Cache-Control no-cache;
        }

        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root /usr/local/nginx/module/nginx-rtmp-module/;
        }

        location /control {
            rtmp_control all;
        }

        #location /publish {
        #    return 201;
        #}

        #location /play {
        #    return 202;
        #}

        #location /record_done {
        #    return 203;
        #}

        location /rtmp-publisher {
            root /usr/local/nginx/module/nginx-rtmp-module/test;
        }

        location / {
            root /usr/local/nginx/module/nginx-rtmp-module/test/www;
        }
    }
}

2.修改文件的默认路径
首先对以下三个文件的用法进行说明:
1).index文件里边有两个作用:一是在直播推流的时候,以OBS为例若服务器和串流秘钥如果填的是以下图例中对应的 服务器:myapp,串流秘钥:test的话,推流的过程中会在直播对应的文件夹下新建一个test的文件夹用于放m3u8文件的碎片,相对应的若是在浏览器中输入http://域名/index.html点击播放之后会定向到直播的内容上;二是在record.html这个文件对应的flie中若index.html文件的第二个参数和record.html的file文件参数一样的话,则进入到http://域名/record.html会进行直播 此时打开http://域名/index.html文件会直接进行观看,且这种的直播实测在4秒左右,而遵从hls协议生成m3u8的这种直播演示起码会在30秒以上。
2).record.html文件定义的是相当于频道,只要record.html文件和index.html文件相互参考同在myapp的test下边的时候在htt[😕/域名/record.html下边的直播可以直接在http://域名/index.html下边直接进行观看,且这种的直播推流是实时的 实测没有对应的文件缓存;不同于m3u8,m3u8需要进行文件的切片以及重组。
3).player.html文件定义的是点播的地址,除去直接在浏览器上直接用http://域名/rtmp-publisher/player.html直接打开进行观看之外,最好的方式是用VLC网络串流的方式rtmp://域名/vod/test.mp4打开,用rtmp://域名:1935/vod/test.mp4也可打开。

[root@tecent ~]#  vim  /usr/local/nginx/module/nginx-rtmp-module/test/www/index.html

在这里插入图片描述

[root@tecent ~]#  vim  /usr/local/nginx/module/nginx-rtmp-module/test/www/record.html

在这里插入图片描述
上传一个mp4格式的视频到/usr/local/nginx/vod/flvs下,此处以test.mp4为例,这是视频点播的地址,观看地址为:http://192.168.0.104/rmtp-publisher/player.html 这是点播的地址

[root@tecent ~]#  vim  /usr/local/nginx/module/nginx-rtmp-module/test/rtmp-publisher/player.html

在这里插入图片描述
3.放行端口

[root@tecent ~]# firewall-cmd --add-port=80/tcp --permanent
[root@tecent ~]# firewall-cmd --add-port=1935/tcp --permanent
[root@tecent ~]# firewall-cmd --reload

4.重载配置

[root@tecent ~]#  /usr/local/nginx/sbin/nginx -s reload(稳妥起见先关服务再开启服务)

四、测试、推流端以及拉流端的配置

输入服务器的ip地址,出现以下画面
在这里插入图片描述

配置OBS推流
在这里插入图片描述
此处推成功之后在浏览器上http://192.168.0.104/stat 可看到流量的详细信息,接下来点击上上布的开始按钮,会开始播放在obs上推出的画面,rtmp流测试完毕。接下来测试通过HLS协议生成的m3u8数据片格式,此种可用VLC播放器对m3u8文件进行解码即可。另外放一个VLC的下载地址:https://mirrors.tuna.tsinghua.edu.cn/videolan-ftp/vlc/3.0.8/win64/vlc-3.0.8-win64.exe
配置VLC(live的路径在nginx中有配置,定位到了/usr/local/nginx/myapp这个目录下,live后面跟的地址为在OBS中配置的密钥,实际上这个密会以此名称在myapp这个目录下生成对应的目录,这个目录下则为需组合的m3u8文件):
在这里插入图片描述

五、最后需要处理的尾巴

  1. 是在nginx的配置文件中已经提到庀jnginx的版本号;
  2. 和1相同,本来在流量统计界面红色区域有nginx和rtmp模块的版本信息(这是处理之后的,处理之前忘了截图),建议关闭,具体需要处理的文件是/usr/local/nginx/module/nginx-rtmp-module/stat.xsl
    在这里插入图片描述
    3.还有认识我的各位大佬应该都知道我两台服务器的地址,就不用帮我安全测试了[捂脸][捂脸]
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值