Skip to content

NodeJs直播服务器搭建

在这里我们用到的直播服务端的开源是Node-Media-Server

安装

首先创建一个node项目

shell

# 初始化项目
npm init -y

# 引入 node-media-server

npm i node-media-server

在入口文件进行启动当前的项目,初始化的项目中默认没有入口文件,需要自己手动添加

js
// index.js

const NodeMediaServer = require("node-media-server")
// 配置文件


const config = {
    // 推流地址
    rtmp: {
      port: 1935,
      chunk_size: 60000,
      gop_cache: true,
      ping: 30,
      ping_timeout: 60
    },
    // 拉流地址
    http: {
      port: 1936,
      allow_origin: '*'
    },
    // 鉴权功能
    auth: {
      play: true,
      publish: true,
      secret: 'nodemedia2017privatekey'
    }
}

let nms = new NodeMediaServer(config)

nms.run()

使用命令

shell

node index.js

当前项目启动成功的提示如下

2021-1-3 19:07:18 73695 [INFO] Node Media Server v2.2.4
2021-1-3 19:07:18 73695 [INFO] Node Media Rtmp Server started on port: 1935
2021-1-3 19:07:18 73695 [INFO] Node Media Http Server started on port: 1936
2021-1-3 19:07:18 73695 [INFO] Node Media WebSocket Server started on port: 1936

表示整个直播服务器就搭建成功了

推流地址为:rtmp://server_id:1935/live/流地址

例如:rtmp://127.0.0.1:1935/live/stream

拉流地址:http://server_id:1936/live/流地址.flv

例如:http://127.0.0.1:1936/live/stream.flv

关于Node配置直播服务器的部分暂时就说这么多,具体的自行参考官方文档Node-Media-Server

使用nginx搭建直播服务器

首先需要准备的模块如下:

我们采用宝塔环境去搭建一个直播nginx的服务器,采用编译模式安装nginx,安装之前手动添加下面的插件其中之一即可

这两个插件不能同时使用

nginx-rtmp-module

--add-module=/www/server/nginx-modal/nginx-rtmp-module

下面的插件包含了上面的插件,只需要安装下面的插件即可

nginx-http-flv-module

--add-module=/www/server/nginx-modal/nginx-http-flv-module

nginx中添加配置文件如下:

nginx

rtmp{
  server{
    listen 1935;
    chunk_size 4000;
    notify_method get;
    application live {
        live on;
        publish_notify on; # 通知地址
        on_publish http://118.25.128.133:8899/index.php; # 需要通知到鉴权的地址
        record off; # 是否录制
        gop_cache on;
       
    }

    application hls{
        live on;
        publish_notify on; # 通知地址
        on_publish http://118.25.128.133:8899/index.php; # 需要通知到鉴权的地址
        record off; # 是否录制
        hls on; # 这个参数把直播服务器改造后成实时回放服务器
        wait_key on; # 对视频切片进行保护,这样就不会产生马赛克了
        hls_path /www/wwwroot/live.28yanyu.cn/live; # 切片文件保存的位置
        hls_fragment 10s; # 每个视频切片的时长
        hls_playlist_length 60s; # 可以回看的时间,这里设置的是1分钟
        hls_continuous on; #连续模式。
        hls_cleanup on;    #对多余的切片进行删除。
        hls_nested on;     #嵌套模式。
    }
  }
}

http{
    server{
        listen 8099;
        server_name localhost;
        # hls 播放模式 访问地址为:http://server_ip:port/hls/流地址.m3u8
        location /hls {      
            types{
                application/vnd.apple.mpegurl m3u8;    
                video/mp2t ts;    
            }
            alias /www/wwwroot/live.28yanyu.cn/live;
            add_header Cache-Control no-cache;
            expires -1;
        }

        # flv播放模式 http://server_ip:port/live?app=appName&port=端口号&stream=流地址
        location /live {
            flv_live on;
            chunked_transfer_encoding  on;
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
	    }

        location /stat {     #第二处添加的location字段。 # 使用http://localhost:8099/stat就可查看是否配置成功
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl { #第二处添加的location字段。
            root /usr/local/nginx/nginx-rtmp-module/; # 这里填写的是nginx-rtmp的路径
        }
    }
   
}


Released under the MIT License.