zoukankan      html  css  js  c++  java
  • 基于Nginx搭建RTMP-HLS视频直播服务器

    基于Nginx搭建RTMP-HLS视频直播服务器

    参考博客:https://www.jianshu.com/p/6d74e47cdd2b
    https://www.cnblogs.com/ebay/p/9968239.html

    一、环境准备

    1.1.1 系统环境

    [root@node ~]# cat /etc/redhat-release 
    CentOS Linux release 7.7.1908 (Core)
    

    1.1.2 依赖环境安装

    [root@node ~]# yum install git gcc gcc-c++ openssl openssl-devel pcre pcre-devel zlib zlib-devel -y
    

    二、软件安装

    2.1.1 Git nginx-rtmp-module项目到本地 安装Nginx的rtmp协议的扩展

    [root@node ~]# cd /usr/local/
    [root@node /usr/local]# git clone https://github.com/arut/nginx-rtmp-module.git
    Cloning into 'nginx-rtmp-module'...
    remote: Enumerating objects: 4314, done.
    remote: Total 4314 (delta 0), reused 0 (delta 0), pack-reused 4314
    Receiving objects: 100% (4314/4314), 3.10 MiB | 289.00 KiB/s, done.
    Resolving deltas: 100% (2686/2686), done.
    [root@node /usr/local]# ll
    total 4
    drwxr-xr-x. 2 root root    6 Apr 11  2018 bin
    drwxr-xr-x. 2 root root    6 Apr 11  2018 etc
    drwxr-xr-x. 2 root root    6 Apr 11  2018 games
    drwxr-xr-x. 2 root root    6 Apr 11  2018 include
    drwxr-xr-x. 2 root root    6 Apr 11  2018 lib
    drwxr-xr-x. 2 root root    6 Apr 11  2018 lib64
    drwxr-xr-x. 2 root root    6 Apr 11  2018 libexec
    drwxr-xr-x  7 root root 4096 May  8 11:56 nginx-rtmp-module
    drwxr-xr-x. 2 root root    6 Apr 11  2018 sbin
    drwxr-xr-x. 5 root root   49 Mar 26 22:35 share
    drwxr-xr-x. 2 root root    6 Apr 11  2018 src
    

    2.1.2 安装nginx

    下载地址:nginx.org或http://nginx.org/download/

    [root@node /usr/local]# wget http://nginx.org/download/nginx-1.15.0.tar.gz
    --2020-05-08 13:10:54--  http://nginx.org/download/nginx-1.15.0.tar.gz
    Resolving nginx.org (nginx.org)... 95.211.80.227, 62.210.92.35, 2001:1af8:4060:a004:21::e3
    Connecting to nginx.org (nginx.org)|95.211.80.227|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 1020675 (997K) [application/octet-stream]
    Saving to: ‘nginx-1.15.0.tar.gz’
    
    100%[===========================================================================================>] 1,020,675   9.75KB/s   in 1m 41s 
    
    2020-05-08 13:12:36 (9.86 KB/s) - ‘nginx-1.15.0.tar.gz’ saved [1020675/1020675]
    
    [root@node /usr/local]# ll
    total 1004
    drwxr-xr-x. 2 root root       6 Apr 11  2018 bin
    drwxr-xr-x. 2 root root       6 Apr 11  2018 etc
    drwxr-xr-x. 2 root root       6 Apr 11  2018 games
    drwxr-xr-x. 2 root root       6 Apr 11  2018 include
    drwxr-xr-x. 2 root root       6 Apr 11  2018 lib
    drwxr-xr-x. 2 root root       6 Apr 11  2018 lib64
    drwxr-xr-x. 2 root root       6 Apr 11  2018 libexec
    -rw-r--r--  1 root root 1020675 Jun  5  2018 nginx-1.15.0.tar.gz
    drwxr-xr-x  7 root root    4096 May  8 11:56 nginx-rtmp-module
    drwxr-xr-x. 2 root root       6 Apr 11  2018 sbin
    drwxr-xr-x. 5 root root      49 Mar 26 22:35 share
    drwxr-xr-x. 2 root root       6 Apr 11  2018 src
    

    解压安装

    [root@node /usr/local]# tar zxvf nginx-1.15.0.tar.gz 
    nginx-1.15.0/
    nginx-1.15.0/auto/
    nginx-1.15.0/conf/
    nginx-1.15.0/contrib/
    nginx-1.15.0/src/
    .....。
    

    查看编译模块

    [root@node /usr/local/nginx-1.15.0]# ./configure --help
    
      --help                             print this message
    
      --prefix=PATH                      set installation prefix
      --sbin-path=PATH                   set nginx binary pathname
      --modules-path=PATH                set modules path
      --conf-path=PATH                   set nginx.conf pathname
      --error-log-path=PATH              set error log pathname
    
    [root@node /usr/local/nginx-1.15.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module 
    > --with-http_stub_status_module --add-module=../nginx-rtmp-module
    checking for OS
     + Linux 3.10.0-1062.18.1.el7.x86_64 x86_64
    checking for C compiler ... found
     + using GNU C compiler
     + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
     
     [root@node /usr/local/nginx-1.15.0]# make && make install
     
     返回上一级目录查看是否安装成功
    [root@node /usr/local/nginx-1.15.0]# cd ..
    [root@node /usr/local]# ls 
    bin  etc  games  include  lib  lib64  libexec  nginx  nginx-1.15.0  nginx-1.15.0.tar.gz  nginx-rtmp-module  sbin  share  src
    [root@node /usr/local]# cd nginx
    [root@node /usr/local/nginx]# sbin/nginx -V
    nginx version: nginx/1.15.0
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
    built with OpenSSL 1.0.2k-fips  26 Jan 2017
    TLS SNI support enabled
    configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --add-module=../nginx-rtmp-module
    [root@node /usr/local/nginx]# sbin/nginx 
    [root@node /usr/local/nginx]# ps -ef|grep nginx
    root      20766      1  0 14:07 ?        00:00:00 nginx: master process sbin/nginx
    nginx     20767  20766  0 14:07 ?        00:00:00 nginx: worker process
    root      20771   1937  0 14:07 pts/0    00:00:00 grep --color=auto nginx
    
    

    2.1.3 创建nginx用户和组

    [root@node /usr/local/nginx-1.15.0]# useradd -M -s /sbin/nologig nginx
    [root@node /usr/local/nginx-1.15.0]# id nginx 
    uid=1001(nginx) gid=1001(nginx) groups=1001(nginx)
    

    三、nginx部署支持rtmp协议

    3.1.2 配置nginx.conf文件

    [root@node /usr/local/nginx]# vim conf/nginx.conf
    #配置如下
    rtmp {
    
          server {
              listen 1935;
              chunk_size 4096;
              application hls {
             }
              chunk_size 4096;
              application hls {   #可以写多个
                  live on;
                  record off;
                  hls_path /usr/local/nginx/html/hls;
                  hls_fragment 3s;
                  hls_fragment 3s;
             }
        
       }
    
    }
    
    
    #完整配置如下
    [root@node /usr/local/nginx]# grep -Ev "#|^$" /usr/local/nginx/conf/nginx.conf
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    rtmp { 
          server {
              listen 1935;
              chunk_size 4096;
              application hls {
                  live on;
                  hls on;
                  hls_path /usr/local/nginx/html/hls;
                  hls_fragment 3s;
             }
        
       }
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  localhost;
            location / {
                root   html;
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
            location /hls {
                types {
                    application/vnd.apple.mpegurl m3u8;
                }
                alias /usr/local/nginx/html/hls; 
                expires -1;
                add_header Cache-Control no-cache;
           } 
           location /stat {
                rtmp_stat all;
                rtmp_stat_stylesheet stat.xsl;
           }
           location /stat.xsl {
               root /usr/local/extend_module/nginx-rtmp-module/;
            }
        }
    }
    

    重新加载一下配置文件

    [root@node /usr/local/nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    

    启动服务

    [root@node /usr/local/nginx]# /usr/local/nginx/sbin/nginx
    [root@node /usr/local/nginx]# ps -ef | grep nginx
    root      20979   1937  0 15:44 pts/0    00:00:00 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    root      20990   1937  0 15:49 pts/0    00:00:00 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    root      21050      1  0 16:07 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
    nginx     21051  21050  0 16:07 ?        00:00:00 nginx: worker process
    nginx     21052  21050  0 16:07 ?        00:00:00 nginx: cache manager process
    

    客户端推送

    直播推流端使用rtmp协议推流,端口为1935。URL格式为:rtmp://ip:端口/hls。推流软件推荐使用开源的OBS。https://obsproject.com/
    流名称要与写的观看直播的页面中的xxxx.m3u8名称一致
    

    查看直播

    浏览器输入http:/xx.xx.xx.xx/hls/test.m3u8就能看直播了
    

    PC端_HLS播放源代码

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>PC HLS video</title>
        <link href="http://cdn.bootcss.com/video.js/6.0.0-RC.5/alt/video-js-cdn.min.css" rel="stylesheet">
    </head>
    <body>
    
    <h1>PC 端播放 HLS(<code>.m3u8</code>) 视频</h1>
    <p>借助 video.js 和 videojs-contrib-hls</p>
    <p>由于 videojs-contrib-hls 需要通过 XHR 来获取解析 m3u8 文件, 因此会遭遇跨域问题, 请设置浏览器运行跨域</p>
    
    <video id="hls-video" width="300" height="200" class="video-js vjs-default-skin"
           playsinline webkit-playsinline
           autoplay controls preload="auto"
           x-webkit-airplay="true" x5-video-player-fullscreen="true" x5-video-player-typ="h5">
        <!-- 直播的视频源 -->
        <source src="http://live.zzbtv.com:80/live/live123/800K/tzwj_video.m3u8" type="application/x-mpegURL">
        <!-- 点播的视频源 -->
        <!--<source src="http://devstreaming.apple.com/videos/wwdc/2015/413eflf3lrh1tyo/413/hls_vod_mvp.m3u8" type="application/x-mpegURL">-->
    </video>
    
    <script src="http://cdn.bootcss.com/video.js/6.0.0-RC.5/video.js"></script>
    <!-- PC 端浏览器不支持播放 hls 文件(m3u8), 需要 videojs-contrib-hls 来给我们解码 -->
    <script src="http://cdn.bootcss.com/videojs-contrib-hls/5.3.3/videojs-contrib-hls.js"></script>
    <script>
        // XMLHttpRequest cannot load http://xxx/video.m3u8. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.198.98:8000' is therefore not allowed access.
        // 由于 videojs-contrib-hls 需要通过 XHR 来获取解析 m3u8 文件, 因此会遭遇跨域问题, 请设置浏览器运行跨域
        var player = videojs('hls-video');
        player.play();
    </script>
    </body>
    </html>
    
  • 相关阅读:
    【转载】关于Java String, StringBuilder, StringBuffer, Hashtable, HashMap的面试题
    LeetCode: 【L4】N-Queens 解题报告
    【转载】在美国找工作秘籍
    Lintcode: Kth Largest Element 解题报告
    LeetCode: Reverse Integer 解题报告
    Lintcode: First Bad Version 解题报告
    九章面试题:Find first K frequency numbers 解题报告
    tomcat之虚拟目录
    百度搜索结果如何屏蔽百家号内容
    CentOS7之Rsync+Inotify架构实现实时同步文件和文件夹
  • 原文地址:https://www.cnblogs.com/woaiyunwei/p/13128917.html
Copyright © 2011-2022 走看看