通过Nodejs搭建流媒体服务--保存视频及监听事件回调

通过Nodejs搭建流媒体服务--保存视频及监听事件回调


本章节中通过流媒体保存视频及监听事件回调,可以做到保存视频及查看访问人数等相关信息。

  • 相关代码
const NodeMediaServer = require('node-media-server');
var md5 =require('md5')
var  appconfig =require('./module/config')

const config = {
    rtmp: {
        port: 1935,
        chunk_size: 60000,
        gop_cache: true,
        ping: 30,
        ping_timeout: 60
    },
    http: {
        port: 8000,
        allow_origin: '*',
        mediaroot: 'media' //录制视频保存路径
    },
    trans: {
        ffmpeg: 'D:\\ffmpeg\\bin\\ffmpeg.exe',//ffmpeg路径  采用双斜杠
        tasks: [
            {
                app: 'live',//app名称
                mp4: true,
                ac: 'aac',
                mp4Flags: '[movflags=faststart]',
            }
        ]
    },
    //开启鉴权验证
    auth: {
        play: true,
        publish: true,
        secret:appconfig.secret
    }
};
var nms = new NodeMediaServer(config)
nms.run();

//生成推流地址
var streamName='haizhengzheng';
var expireData=parseInt((Date.now()+1000000)/1000)
var HashValue=md5(`/live/${streamName}-${expireData}-${appconfig.secret}`)
var sign=`${expireData}-${HashValue}`;
var rtmpurl =`rtmp://192.168.0.105/live/${streamName}?sign=${sign}`
console.log(rtmpurl)

//事件回调监听
nms.on('preConnect', (id, args) => {
   console.log('[NodeEvent on preConnect]', `id=${id} args=${JSON.stringify(args)}`);
// let session = nms.getSession(id);
// session.reject();
})
nms.on('postConnect', (id, args) => {
    console.log('[NodeEvent on postConnect]', `id=${id} args=${JSON.stringify(args)}`);
});
nms.on('doneConnect', (id, args) => {
    console.log('[NodeEvent on doneConnect]', `id=${id} args=${JSON.stringify(args)}`);
});
nms.on('prePublish', (id, StreamPath, args) => {
    console.log('[NodeEvent on prePublish]', `id=${id} StreamPath=${StreamPath}
args=${JSON.stringify(args)}`);
// let session = nms.getSession(id);
// session.reject();
});
nms.on('postPublish', (id, StreamPath, args) => {
    console.log('[NodeEvent on postPublish]', `id=${id} StreamPath=${StreamPath}
args=${JSON.stringify(args)}`);
});
nms.on('donePublish', (id, StreamPath, args) => {
    console.log('[NodeEvent on donePublish]', `id=${id} StreamPath=${StreamPath}
args=${JSON.stringify(args)}`);
});
nms.on('prePlay', (id, StreamPath, args) => {
    console.log('[NodeEvent on prePlay]', `id=${id} StreamPath=${StreamPath}
args=${JSON.stringify(args)}`);
// let session = nms.getSession(id);
// session.reject();
});
nms.on('postPlay', (id, StreamPath, args) => {
    console.log('[NodeEvent on postPlay]', `id=${id} StreamPath=${StreamPath}
args=${JSON.stringify(args)}`);
});
nms.on('donePlay', (id, StreamPath, args) => {
    console.log('[NodeEvent on donePlay]', `id=${id} StreamPath=${StreamPath}
args=${JSON.stringify(args)}`);
});
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值