通过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)}`);
});
发布了31 篇原创文章 · 获赞 29 · 访问量 8735

猜你喜欢

转载自blog.csdn.net/m0_38051293/article/details/104113247
今日推荐