0

0

0

修罗

站点介绍

只有了解事实才能获得真正的自由

直播推拉流 - Node-Media-Server

修罗 2022-01-03 2140 0条评论 nodejs

首页 / 正文

文档:https://github.com/illuspas/Node-Media-Server/blob/master/README_CN.md

安装 node-media-server

npm i node-media-server

一. 创建推拉流服务器

1. 新建app.js文件

// app.js
const NodeMediaServer = require('node-media-server');

const config = {
  rtmp: {
    port: 19351,
    chunk_size: 60000,
    gop_cache: true,
    ping: 30,
    ping_timeout: 60
  },
  http: {
    port: 19352,
    allow_origin: '*'
  }
};

var nms = new NodeMediaServer(config)
nms.run();

1641192620225.png

2. 启动app.js

node app.js

1641192674874.png

3. 推流地址和拉流地址

# 推流:
rtmp://localhost:19351/live/STREAM_NAME
# 拉流:
http://localhost:19352/live/STREAM_NAME.flv

STREAM_NAME 自定义名字。

二. 使用obs推流

安装obs软件:https://obsproject.com/

1. 添加采集目标

打开obs软件,添加显示器作为采集目标

1641193055816.png

2. 设置推流地址

点击软件左上角 文件->设置->推流,设置推流地址

1641193361670.png

3. 开始推流

  • 点击开始推流

1641193406056.png

  • 开启推流成功状态

1641193526427.png

三. 拉流

  • 新建文件index.html
<script src="https://cdn.bootcss.com/flv.js/1.5.0/flv.min.js"></script>
<video id="videoElement"></video>
<script>
    if (flvjs.isSupported()) {
        var videoElement = document.getElementById('videoElement');
        var flvPlayer = flvjs.createPlayer({
            type: 'flv',
            url: 'http://127.0.0.1:19352/live/hello.flv'
        });
        flvPlayer.attachMediaElement(videoElement);
        flvPlayer.load();
        flvPlayer.play();
    }
</script>

1641193605439.png

  • 通过浏览器打开编写的html,拉流成功

1641193686248.png

四. 鉴权验证

加密后的 URL 形式:

rtmp://hostname:port/live/hello?sign=expires-HashValue
http://hostname:port/live/hello.flv?sign=expires-HashValue

1.原始推流或播放地址:

rtmp://192.168.0.10/live/hello

2.配置验证秘钥为: 'nodemedia2017privatekey',同时打开播放和发布的鉴权开关

const config = {
  rtmp: {
    port: 1935,
    chunk_size: 60000,
    gop_cache: true,
    ping: 30,
    ping_timeout: 60
  },
  http: {
    port: 8000,
    allow_origin: '*'
  },
  auth: {
    play: true,
    publish: true,
    secret: 'nodemedia2017privatekey'
  }
}

3.请求过期时间为: 2022/8/23 11:25:21 ,则请求过期时间戳为:

1661225121 单位为秒;

4.md5计算结合“完整流地址-失效时间-密钥”的字符串:

HashValue = md5("/live/hello-1661225121-nodemedia2017privatekey”)
HashValue = 80c1d1ad2e0c2ab63eebb50eed64201a

5.最终请求地址为

rtmp://192.168.0.10/live/hello?sign=1661225121-80c1d1ad2e0c2ab63eebb50eed64201a
注意:'sign' 关键字不能修改为其他的

例:

计算推拉流地址

function createSign(myKey){
    const expireTime = parseInt((Date.now() + 100000000) / 1000)
    const hashValue = md5(`/live/${myKey}-${expireTime}-${config.auth.secret}`)
    return `${expireTime}-${hashValue}` 
}

// 可以修改hello为其他名字生成不同直播间,这里写死了"hello"
const myKey = "hello"
console.log(createSign(myKey)); // 1641295664-36cb23cec1eafa8de108c6299f1f0d6e

推流地址

rtmp://127.0.0.1:19351/live/hello?sign=1641295664-36cb23cec1eafa8de108c6299f1f0d6e

1641196145650.png

拉流地址

http://127.0.0.1:19352/live/hello.flv?sign=1641295664-36cb23cec1eafa8de108c6299f1f0d6e

评论(0)


最新评论

  • 1

    1

  • 1

    1

  • -1' OR 2+158-158-1=0+0+0+1 or 'TKCTZnRa'='

    1

  • 1

    1

  • 1

    1

  • 1

    1

  • 1

    1

  • @@5Qa2D

    1

  • 1

    1

  • 1

    1

日历

2025年09月

 123456
78910111213
14151617181920
21222324252627
282930    

文章目录

推荐关键字: Linux webpack js 算法 MongoDB laravel JAVA jquery javase redis