基於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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM