https://github.com/illuspas/Node-Media-Server/blob/master/README_CN.md
const NodeMediaServer = require('node-media-server');
const { exec } = require('child_process');
const config = {
rtmp: {
port: 1935,
chunk_size: 60000,
gop_cache: true,
ping: 30,
ping_timeout: 60
},
auth: {play: true, publish: true, secret: '123456789'},
http: {
port: 8000,
mediaroot: './media',
allow_origin: '*'
},
trans: {
ffmpeg: 'd:/ffmpeg/bin/ffmpeg.exe',
tasks: [{
app: 'live',
hls: true,
hlsFlags: '[hls_time=2:hls_list_size=3:hls_flags=delete_segments]',
dash: true,
dashFlags: '[f=dash:window_size=3:extra_window_size=5]'
}]
}
};
var nms = new NodeMediaServer(config)
nms.run();
nms.on('prePublish', (id, StreamPath, args) => {
//console.log(`[NodeEvent on prePublish] ${id} ${StreamPath} ${args}`);
const outputFilePath = `./media/${StreamPath}.mp4`;
const ffmpegCommand = `d:/ffmpeg/bin/ffmpeg.exe -i rtmp://localhost:1935${StreamPath} -c:v copy -c:a copy ${outputFilePath}`;
exec(ffmpegCommand, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});
});