This repository was archived by the owner on Aug 27, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathrtsp-ffmpeg.js
More file actions
151 lines (135 loc) · 3.84 KB
/
Copy pathrtsp-ffmpeg.js
File metadata and controls
151 lines (135 loc) · 3.84 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Created by Andrew D.Laptev<a.d.laptev@gmail.com> on 30.03.15.
*/
const spawn = require('child_process').spawn,
EventEmitter = require('events').EventEmitter,
util = require('util'),
Buffer = require('buffer').Buffer;
/**
* Stream constructor
* @param {object} options
* @param {string} options.input Stream uri, for example rtsp://r3---sn-5hn7su76.c.youtube.com/CiILENy73wIaGQnup-1SztVOYBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
* @param {number|string} [options.rate] Framerate
* @param {string} [options.resolution] Resolution in WxH format
* @param {string|number} [options.quality] JPEG quality
* @param {Array<string>} [options.arguments] Custom arguments for ffmpeg
* @constructor
*/
var FFMpeg = function (options) {
if (options.input) {
this.input = options.input;
} else {
throw new Error('no `input` parameter');
}
this.rate = options.rate || 10;
this.resolution = options.resolution;
this.quality = (options.quality === undefined || options.quality === "") ? 3 : options.quality;
this.arguments = options.arguments || [];
this.buff = Buffer.from(''); // Store the entire data image into this variable. This attribute is replaced each time a full image is received from the stream.
this.on('newListener', newListener.bind(this));
this.on('removeListener', removeListener.bind(this));
if (Object.observe && (typeof Proxy !== 'function')) {
Object.observe(this, observer.bind(this));
}
};
util.inherits(FFMpeg, EventEmitter);
/**
* FFMpeg command name
* @type {string}
*/
FFMpeg.cmd = 'ffmpeg';
function newListener(event) {
if (event === 'data' && this.listeners(event).length === 0) {
this.start();
}
}
function removeListener(event) {
if (event === 'data' && this.listeners(event).length === 0) {
this.stop();
}
}
function observer(changes) {
if (changes.some(function (change) {
return change.type === 'update';
})) {
this.restart();
}
}
FFMpeg.prototype._args = function () {
return this.arguments.concat([
'-loglevel', 'quiet',
'-hwaccel', 'auto', '-re',
'-f', 'rtsp',
'-rtsp_transport', 'tcp',
'-i', this.input,
'-r', '3',
'-f', 'image2pipe',
'-preset', 'ultrafast',
'-vf', 'scale=640:-1',
'pipe:1'
])
};
/**
* Start ffmpeg spawn process
*/
FFMpeg.prototype.start = function () {
var self = this;
this.child = spawn(FFMpeg.cmd, this._args());
this.child.stdout.on('data', function (data) {
//The image can be composed of one or multiple chunk when receiving stream data.
//Store all bytes into an array until we meet flag "FF D9" that mean it's the end of the image then we can send all data in order to display the full image.
if (data.length > 1) {
self.buff = Buffer.concat([self.buff, data]);
var offset = data[data.length - 2].toString(16);
var offset2 = data[data.length - 1].toString(16);
if (offset == "ff" && offset2 == "d9") {
self.emit('data', self.buff);
self.buff = Buffer.from('');
}
}
});
this.child.stderr.on('data', function (data) {
throw new Error(data);
});
this.emit('start');
this.child.on('close', function (code) {
if (code === 0) {
setTimeout(FFMpeg.prototype.start.bind(this), 1000);
}
}.bind(this));
this.child.on('error', function (err) {
if (err.code === 'ENOENT') {
throw new Error('FFMpeg executable wasn\'t found. Install this package and check FFMpeg.cmd property');
} else {
throw err;
}
});
};
/**
* Stop ffmpeg spawn process
*/
FFMpeg.prototype.stop = function () {
this.child.kill();
delete this.child;
this.emit('stop');
};
/**
* Restart ffmpeg spawn process
*/
FFMpeg.prototype.restart = function () {
if (this.child) {
this.stop();
this.start();
}
};
if (typeof Proxy === 'function') {
FFMpeg = new Proxy(FFMpeg, {
set: function (target, property) {
if (property !== 'super_' && target[property] !== undefined) {
target.restart();
}
return true;
}
});
}
module.exports.FFMpeg = FFMpeg;