加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
node_trans_session.js 2.99 KB
一键复制 编辑 原始数据 按行查看 历史
illuspas 提交于 2018-06-19 10:14 . u
//
// Created by Mingliang Chen on 18/3/9.
// illuspas[a]gmail.com
// Copyright (c) 2018 Nodemedia. All rights reserved.
//
const Logger = require('./node_core_logger');
const EventEmitter = require('events');
const { spawn } = require('child_process');
const dateFormat = require('dateformat');
const mkdirp = require('mkdirp');
const fs = require('fs');
class NodeTransSession extends EventEmitter {
constructor(conf) {
super();
this.conf = conf;
}
run() {
let vc = 'copy';
let ac = this.conf.args.ac == 10 ? 'copy' : this.conf.ac ? this.conf.ac : 'aac';
let inPath = 'rtmp://127.0.0.1:' + this.conf.port + this.conf.streamPath;
let ouPath = `${this.conf.mediaroot}/${this.conf.app}/${this.conf.stream}`;
let mapStr = '';
if (this.conf.mp4) {
this.conf.mp4Flags = this.conf.mp4Flags ? this.conf.mp4Flags : '';
let now = new Date();
let mp4FileName = dateFormat('yyyy-mm-dd-HH-MM') + '.mp4';
let mapMp4 = `${this.conf.mp4Flags}${ouPath}/${mp4FileName}|`;
mapStr += mapMp4;
Logger.log('[Transmuxing MP4] ' + this.conf.streamPath + ' to ' + ouPath + '/' + mp4FileName);
}
if (this.conf.hls) {
this.conf.hlsFlags = this.conf.hlsFlags ? this.conf.hlsFlags : '';
let hlsFileName = 'index.m3u8';
let mapHls = `${this.conf.hlsFlags}${ouPath}/${hlsFileName}|`;
mapStr += mapHls;
Logger.log('[Transmuxing HLS] ' + this.conf.streamPath + ' to ' + ouPath + '/' + hlsFileName);
}
if (this.conf.dash) {
this.conf.dashFlags = this.conf.dashFlags ? this.conf.dashFlags : '';
let dashFileName = 'index.mpd';
let mapDash = `${this.conf.dashFlags}${ouPath}/${dashFileName}`;
mapStr += mapDash;
Logger.log('[Transmuxing DASH] ' + this.conf.streamPath + ' to ' + ouPath + '/' + dashFileName);
}
mkdirp.sync(ouPath);
let argv = ['-y', '-fflags', 'nobuffer', '-analyzeduration', '1000000', '-i', inPath, '-c:v', vc, '-c:a', ac, '-f', 'tee', '-map', '0:a?', '-map', '0:v?', mapStr];
Logger.ffdebug(argv.toString());
this.ffmpeg_exec = spawn(this.conf.ffmpeg, argv);
this.ffmpeg_exec.on('error', (e) => {
Logger.ffdebug(e);
});
this.ffmpeg_exec.stdout.on('data', (data) => {
Logger.ffdebug(`FF输出:${data}`);
});
this.ffmpeg_exec.stderr.on('data', (data) => {
Logger.ffdebug(`FF输出:${data}`);
});
this.ffmpeg_exec.on('close', (code) => {
Logger.log('[Transmuxing end] ' + this.conf.streamPath);
this.emit('end');
fs.readdir(ouPath, function (err, files) {
if (!err) {
files.forEach((filename) => {
if (filename.endsWith('.ts')
|| filename.endsWith('.m3u8')
|| filename.endsWith('.mpd')
|| filename.endsWith('.m4s')) {
fs.unlinkSync(ouPath + '/' + filename);
}
})
}
});
});
}
end() {
// this.ffmpeg_exec.kill('SIGINT');
this.ffmpeg_exec.stdin.write('q');
}
}
module.exports = NodeTransSession;
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化