-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRTSPRecorder.js
218 lines (186 loc) · 6.1 KB
/
RTSPRecorder.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* Created by Shoom on 02.12.15.
*/
(function(){
var
fs = require('fs'),
child_process = require('child_process'),
du = require('du'),
async = require('async');
/**
* Date to string
* @param date {Date|undefined}
* @returns {string}
*/
function dateString(date){
var dt = date || (new Date());
return [dt.getDate(), dt.getMonth(), dt.getFullYear()].join('-')+' '
+[dt.getHours(), dt.getMinutes(), dt.getSeconds()].join('-');
}
/**
* Remove folder recursive
* @param location {string} dir location
* @param next {function} callback
*/
function removeFolder(location, next) {
fs.readdir(location, function (err, files) {
async.each(files, function (file, cb) {
file = location + '/' + file;
fs.stat(file, function (err, stat) {
if (err) {
return cb(err);
}
if (stat.isDirectory()) {
removeFolder(file, cb);
} else {
fs.unlink(file, function (err) {
if (err) {
return cb(err);
}
return cb();
})
}
})
}, function (err) {
if (err) return next(err);
fs.rmdir(location, function (err) {
return next(err);
})
})
})
}
var protocol = 'rtsp://';
var urlRegex = new RegExp(protocol+"([\\w\\d]+):([\\w\\d]+)@");
/**
* Rtsp stream recorder and streamer
* @param params {object} parameters
* @param name {string} name if recorder
* @constructor
*/
var Recorder = function(params, name){
this.name = name || '';
//url to stream
this.url = '';
//max size of video directory (MB), if size more than this size dir will be cleared
this.maxDirSize = 100;
//limit to record one video file (sec)
this.timeLimit = 600;
//folder of videos
this.folder = '/';
params = params || {};
for(var v in params){
if(params.hasOwnProperty(v)) this[v] = params[v];
}
if(!this.username){
var regres = urlRegex.exec(this.url);
this.url = this.url.replace(regres[0], '');
this.username = regres[1];
this.password = regres[2];
}
var self = this;
/**
* Logging
*/
this.log = function(){
for(var i in arguments){
arguments[i] = dateString()+':: '+arguments[i].toString();
}
console.log.apply(this, arguments);
return this;
};
/**
* Path to records folder
* @returns {string}
*/
this.recordsPath = function(){
return this.folder+(this.name?(this.name+'/'):'');
};
this.ffmpeg = function(filename){
return child_process.spawn("ffmpeg",
["-i", protocol+this.username+':'+this.password+'@'+this.url, '-an', '-f', 'mpeg1video', '-b:v', '128k', '-r', '25', filename],
{detached: false}
);
};
/**
* Record stream to file
*/
this.recordStream = function(){
if(this.timer) clearTimeout(this.timer);
if(this.writeStream && this.writeStream.binded) return false;
if(this.writeStream && this.writeStream.connected){
this.writeStream.binded = true;
this.writeStream.once('exit', function(){
self.recordStream();
});
this.writeStream.kill();
return false;
}
this.clearDir(function(){
var filename = this.recordsPath()+dateString()+'.mp4';
this.writeStream = null;
this.writeStream = this.ffmpeg(filename);
this.writeStream.once('exit', function(){
self.recordStream();
});
this.timer = setTimeout(function(){
self.writeStream.kill();
}, this.timeLimit*1000);
this.log("Start record "+filename);
});
return this;
};
/**
* Clear movies directory
* @param cb {function} callback
*/
this.clearDir = function(cb){
var called = false;
function ok(){
cb.apply(self);
}
du(this.folder, function (err, size) {
if(size/1024/1024 > self.maxDirSize){
try{
removeFolder(self.folder, function(){
fs.mkdir(self.folder, function(){
if(!called){
ok();
called = true;
}
});
});
}catch (err){
self.log(err);
}
}else{
if(!called){
ok();
called = true;
}
}
});
return this;
};
/**
* Initialize record
* @see reconnect
* @see recordStream
*/
this.initialize = function(){
if(!this.url){
return this.log('URL os required.');
}
//Create records directory if not exist
try{
if(!fs.lstatSync(this.recordsPath()).isDirectory()){
fs.mkdirSync(this.recordsPath());
}
}catch (e){
fs.mkdirSync(this.recordsPath());
}
self.recordStream();
return this;
};
};
module.exports = Recorder;
})();