forked from illuspas/Node-Media-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_trans_session.js
102 lines (93 loc) · 3.57 KB
/
node_trans_session.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// 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 = this.conf.vc || 'copy';
let ac = this.conf.ac || 'copy';
let inPath = 'rtmp://127.0.0.1:' + this.conf.rtmpPort + this.conf.streamPath;
let ouPath = `${this.conf.mediaroot}/${this.conf.streamApp}/${this.conf.streamName}`;
let mapStr = '';
if (this.conf.rtmp && this.conf.rtmpApp) {
if (this.conf.rtmpApp === this.conf.streamApp) {
Logger.error('[Transmuxing RTMP] Cannot output to the same app.');
} else {
let rtmpOutput = `rtmp://127.0.0.1:${this.conf.rtmpPort}/${this.conf.rtmpApp}/${this.conf.streamName}`;
mapStr += `[f=flv]${rtmpOutput}|`;
Logger.log('[Transmuxing RTMP] ' + this.conf.streamPath + ' to ' + rtmpOutput);
}
}
if (this.conf.mp4) {
this.conf.mp4Flags = this.conf.mp4Flags ? this.conf.mp4Flags : '';
let mp4FileName = dateFormat('yyyy-mm-dd-HH-MM-ss') + '.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', '-i', inPath];
Array.prototype.push.apply(argv, ['-c:v', vc]);
Array.prototype.push.apply(argv, this.conf.vcParam);
Array.prototype.push.apply(argv, ['-c:a', ac]);
Array.prototype.push.apply(argv, this.conf.acParam);
Array.prototype.push.apply(argv, ['-f', 'tee', '-map', '0:a?', '-map', '0:v?', mapStr]);
argv = argv.filter((n) => { return n }); //去空
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')
|| filename.endsWith('.tmp')) {
fs.unlinkSync(ouPath + '/' + filename);
}
})
}
});
});
}
end() {
this.ffmpeg_exec.kill();
}
}
module.exports = NodeTransSession;