-
Notifications
You must be signed in to change notification settings - Fork 22
/
mpeg1muxer.js
executable file
·93 lines (85 loc) · 2.63 KB
/
mpeg1muxer.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
var Mpeg1Muxer, child_process, events, util
child_process = require('child_process')
util = require('util')
events = require('events')
Mpeg1Muxer = function (options) {
var key
this.url = options.url
this.ffmpegOptions = options.ffmpegOptions
const ffmpegOptionsDefault = {
'-f': 'mpegts',
'-codec:v': 'mpeg1video',
}
this.ffmpegChannelPre = options.ffmpegPreOptions;
this.exitCode = undefined
this.signal = undefined
this.killed = false;
this.additionalFlags = []
this.additionalPreFlags = []
if (this.ffmpegOptions) {
for (key in this.ffmpegOptions) {
if (ffmpegOptionsDefault[key] != null) {
ffmpegOptionsDefault[key] = String(this.ffmpegOptions[key]);
} else {
this.additionalFlags.push(key)
if (String(this.ffmpegOptions[key]) !== '') {
this.additionalFlags.push(String(this.ffmpegOptions[key]))
}
}
}
}
if (this.ffmpegChannelPre) {
for (key in this.ffmpegChannelPre) {
this.additionalPreFlags.push(key)
if (String(this.ffmpegChannelPre[key]) !== '') {
this.additionalPreFlags.push(String(this.ffmpegChannelPre[key]))
}
}
}
if (ffmpegOptionsDefault) {
for (key in ffmpegOptionsDefault) {
this.additionalFlags.push(key)
if (String(ffmpegOptionsDefault[key]) !== '') {
this.additionalFlags.push(String(ffmpegOptionsDefault[key]))
}
}
}
this.spawnOptions = [
...this.additionalPreFlags,
"-i",
this.url,
// additional ffmpeg options go here
...this.additionalFlags,
'-'
]
this.stream = child_process.spawn(options.ffmpegPath, this.spawnOptions, {
detached: false
})
this.kill=()=>{
this.killed=true;
this.stream.kill();
}
this.inputStreamStarted = true
this.stream.stdout.on('data', (data) => {
return this.emit('mpeg1data', data)
})
this.stream.stderr.on('data', (data) => {
return this.emit('ffmpegStderr', data)
})
this.stream.on('exit', (code, signal) => {
if (this.killed){
return ;
}
if (code === 0) {
return this.emit('exitWithoutError')
} else {
console.error('RTSP stream exited with error')
this.exitCode = code
this.signal = signal
return this.emit('exitWithError')
}
})
return this
}
util.inherits(Mpeg1Muxer, events.EventEmitter)
module.exports = Mpeg1Muxer