-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
466 lines (417 loc) · 24.9 KB
/
app.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
'use strict';
const config = require('./config/config.json'); //initial configuration load
const fs = require('fs'); //file stream read write -FUTURE-
const mqttServer = config.mqttServer; //mqtt server IP address
const clientid = config.clientid; //clientid, also used for topic
const mqttmsgoptions = config.mqttOptions;
const FFMPEGupdateTime = config.FFMPEGupdateTime; //universal update FFMPEG time
const ffmpegPath = (config.UseFFmpegManPath) ? config.FFmpegPathManual : require('@ffmpeg-installer/ffmpeg').path; //"C:\\FFMPEG\\bin\\ffmpeg.exe";
const ffmpeg = require('fluent-ffmpeg');
const topic = config.clientid + '/' + config.topic; //publish topic prefix
const defaultMessage = config.defaultMessage; //default message for erros
const mqtt = require('mqtt');
var CAMERA_FFMPEG_PROCESS = [];
var CAMERA_DATA = config.cameras; //camera objects with settings
var mqttSTopics = [clientid + '/restartCamStream', clientid + '/addCamStream',
clientid + '/updateCamStreamSettings', clientid + '/stopCamStream',clientid + '/startCamStream', clientid + '/requestCamStreamData' ,
clientid + '/updateConfig' ];
var mqttSTopicsValues = [];
var mqttCamStatusValues = [];
var mqttsQos = 1;
var mqttSubInterval;
var mqttSubPInterval=[];
ffmpeg.setFfmpegPath(ffmpegPath);
console.log((new Date().toISOString()) + ": ffmpegPath := " + ffmpegPath);
console.log((new Date().toISOString()) + ": camera count := " + config.cameras.length);
//Connect to MQTT Server
var client = mqtt.connect(mqttServer, { clientId: clientid });
//Loop to creat array of cameras for time intervals for FFMPEG Stream Volume Detect
var i;
var j;
var arrayCamTimeIntervals = [];
var arrayFFMPEGCount = [];
var arrayFFMPEGComplete = [];
console.log((new Date().toISOString()) + ": Cam Length Func : " + config.cameras.length);
onStart(j);
function onStart(j) {
loadConfigSettings(i);
//Set Subscribe MQTT Up for Use
var defaultsMQTTST = { 'name': '', 'activate': false, 'data': '', 'response':'' };
client.on("connect", function () {
console.log((new Date().toISOString()) + ": MQTT Status : " + client.connected);
//clear the subscribe topics to defaults
for (j = 0; j < mqttSTopics.length; j++) {
mqttpublish(mqttSTopics[j], JSON.stringify(defaultsMQTTST), mqttmsgoptions, j, function (response) {
console.log((new Date().toISOString()) + ": MQTT Sub Pub : " + mqttSTopics[j] + " is " + response);
(response) ? clearInterval(mqttSubPInterval[j]) : console.log("Waiting");
});
}
//subscribe to all topics in array
mqttsubscribe(mqttSTopics, mqttsQos, function (response) {
console.log((new Date().toISOString()) + ": MQTT Subsribe : " + response);
(response) ? clearInterval(mqttSubInterval) : console.log("Waiting");
});
});
}
function loadConfigSettings(i) {
for (i = 0; i < (config.cameras.length); i++) {
console.log((new Date().toISOString()) + ": Loading Camera :" + CAMERA_DATA[i].name + " Stream:= " + CAMERA_DATA[i].streamURL);
startAnalysis(i, CAMERA_DATA);
}
}
//setInterval(function () { console.log("Array: " + JSON.stringify(arrayCamTimeIntervals)); }, 6000);
//Starts the Volume Detect with setInterval for repective cameras
function startAnalysis(i, CAMERA_DATA) {
arrayFFMPEGCount[CAMERA_DATA[i].name] = 0; //sets the FFMPEG Command count for respective camera
arrayFFMPEGComplete[CAMERA_DATA[i].name] = true; //sets the first run FFMPEG for the respective camera
mqttCamStatusValues[CAMERA_DATA[i].name] = {
'camera': CAMERA_DATA[i].name, 'msgid':0, "volume": { 'max': '', 'mean': '','ffmpeg_stat':"", 'cam_stat': "created", "time": new Date().toISOString() }
};
//creates array of IDS for setInterval for each camera in config.json
arrayCamTimeIntervals[CAMERA_DATA[i].name] =
setInterval(function () {
//Runs check if first run through or if last FFMPEG analysis completed for respective camera
if (arrayFFMPEGComplete[CAMERA_DATA[i].name]) {
arrayFFMPEGCount[CAMERA_DATA[i].name] = 1 + arrayFFMPEGCount[CAMERA_DATA[i].name];
arrayFFMPEGComplete[CAMERA_DATA[i].name] = false;
//Calls the FFMPEG analysis function
getVolumeAnalysis(CAMERA_DATA[i], arrayFFMPEGCount[CAMERA_DATA[i].name], function (statVolume) {
//sets mqtt message for respective camera to returned results
let messageReturn = JSON.stringify({ "volume": { 'max': statVolume.max, 'mean': statVolume.mean, 'ffmpeg_stat': statVolume.status, 'cam_stat': 'running' , "time": statVolume.timestamp } });
//Sets mqtt message to error message if nothing returned
if (!statVolume) {
mqttCamStatusValues[CAMERA_DATA[i].name].volume.ffmpeg_stat = "Unknown Process Error";
messageReturn = JSON.stringify({ "volume": { 'max': mqttCamStatusValues[CAMERA_DATA[i].name].volume.max, 'mean': mqttCamStatusValues[CAMERA_DATA[i].name].volume.mean, 'ffmpeg_stat': mqttCamStatusValues[CAMERA_DATA[i].name].ffmpeg_stat, 'cam_stat': 'running', "time": new Date().toISOString() } });
mqttpublish(topic + "/" + CAMERA_DATA[i].name, messageReturn, mqttmsgoptions, mqttCamStatusValues[CAMERA_DATA[i].name].msgid, function (posted) {
});
}
// Outputs log if an error is reported by FFMPEG
if (statVolume.error) {
console.log((new Date().toISOString()) + ": FFmpeg End Err : " + arrayFFMPEGCount[CAMERA_DATA[i].name] + " : " + CAMERA_DATA[i].name + " Cleared: " + statVolume.errmsg);
mqttCamStatusValues[CAMERA_DATA[i].name].volume.ffmpeg_stat = "Process Error";
mqttCamStatusValues[CAMERA_DATA[i].name].msgid = statVolume.id;
messageReturn = JSON.stringify({ "volume": { 'max': mqttCamStatusValues[CAMERA_DATA[i].name].volume.max, 'mean': mqttCamStatusValues[CAMERA_DATA[i].name].volume.mean, 'ffmpeg_stat': "Process Error", 'cam_stat': 'running', "time": new Date().toISOString() } });
mqttpublish(topic + "/" + CAMERA_DATA[i].name, messageReturn, mqttmsgoptions, statVolume.id, function (posted) {
});
//clearInterval(arrayCamTimeIntervals[CAMERA_DATA[i].name]);
}
// publishes the MQTT message if there is no error and mqtt is connected
if (client.connected && !statVolume.error) {
console.log((new Date().toISOString()) + ": MQTT Processing : " + statVolume.id + ": " + CAMERA_DATA[i].name);
mqttCamStatusValues[CAMERA_DATA[i].name] = {
'camera': CAMERA_DATA[i].name, 'msgid': statVolume.id, "volume": { 'max': statVolume.max, 'mean': statVolume.mean, 'ffmpeg_stat': statVolume.status, 'cam_stat': 'running', "time": statVolume.timestamp }
};
mqttpublish(topic + "/" + CAMERA_DATA[i].name, messageReturn, mqttmsgoptions, statVolume.id, function (posted) {
});
}
// don't do anything if MQTT is not connected
if (!client.connected) {
console.log((new Date().toISOString()) + ": MQTT NOT CONNECTED!");
}
//Mark FFMPEG analysis complete for respective camera
arrayFFMPEGComplete[CAMERA_DATA[i].name] = true;
});
}
}, (CAMERA_DATA[i].UpdateTime * 1000 + 2000));
console.log((new Date().toISOString()) + ": array index : " + CAMERA_DATA[i].name);
}
/************** MQTT Handling Functions *************/
client.on("connect", function () {
console.log((new Date().toISOString()) + ": MQTT Status : " + client.connected);
});
client.on("error", function (error) {
console.log((new Date().toISOString()) + ": MQTT Status : " + error);
});
client.on('message', function (stopic, message, packet) {
mqttSTopicsValues[stopic] = JSON.parse(message); //Parse the mqtt message
console.log((new Date().toISOString()) + ": SubscribeTopic : " + stopic + " : Data set to: " + mqttSTopicsValues[stopic].data);
var responseMQTTST = { 'name': '', 'activate': false, 'data': '', 'response': '' }; //blank response message that clears activate signal
//Add a Camera Stream Analysis from MQTT message
if (stopic == (clientid + "/addCamStream") && mqttSTopicsValues[stopic].data != "") {
addCamStream(mqttSTopicsValues[stopic], function (response) {
responseMQTTST.response = response;
let messageReturn = JSON.stringify({
"volume": {
'max': mqttCamStatusValues[mqttSTopicsValues[stopic].name].volume.max, 'mean': mqttCamStatusValues[mqttSTopicsValues[stopic].name].volume.mean,
'ffmpeg_stat': mqttCamStatusValues[mqttSTopicsValues[stopic].name].volume.ffmpeg_stat, 'cam_stat': responseMQTTST.response, "time": new Date().toISOString()
}
});
//Wait for last FFMPEG Analysis to stop or complete then send the MQTT stopped response
setTimeout(() => {
console.log(mqttSTopicsValues[stopic].name);
mqttpublish(topic + "/" + mqttSTopicsValues[stopic].name, messageReturn, mqttmsgoptions, responseMQTTST.response, function (posted) { });
}, FFMPEGupdateTime * 1000 + 5000);
});
mqttpublish(stopic, JSON.stringify(responseMQTTST), mqttmsgoptions, stopic, function (response) {
//console.log("MQTT subscribed publish: " + mqttSTopics[j] + " is " + response);
//(response) ? clearInterval(mqttSubPInterval[j]) : console.log("Waiting");
});
}
//Stop Camera Stream Analysis from MQTT message
if (stopic == (clientid + "/stopCamStream") && mqttSTopicsValues[stopic].activate) {
stopCamStream(mqttSTopicsValues[stopic], function (response) {
responseMQTTST.response = response;
var camname= mqttSTopicsValues[stopic].name;
let messageReturn = JSON.stringify({
"volume": {
'max': mqttCamStatusValues[mqttSTopicsValues[stopic].name].volume.max, 'mean': mqttCamStatusValues[mqttSTopicsValues[stopic].name].volume.mean,
'ffmpeg_stat': mqttCamStatusValues[mqttSTopicsValues[stopic].name].volume.ffmpeg_stat, 'cam_stat': responseMQTTST.response, "time": new Date().toISOString()
}
});
//Wait for last FFMPEG Analysis to stop or complete then send the MQTT stopped response
setTimeout(() => {
//console.log("camname: " + camname);
mqttpublish(topic + "/" + camname, messageReturn, mqttmsgoptions, responseMQTTST.response, function (posted) { });
}, FFMPEGupdateTime * 1000 + 5000);
});
mqttpublish(stopic, JSON.stringify(responseMQTTST), mqttmsgoptions, stopic, function (response) {
});
}
//Start Camera Stream Analysis from MQTT message
if (stopic == (clientid + "/startCamStream") && mqttSTopicsValues[stopic].activate) {
startCamStream(mqttSTopicsValues[stopic], function (response) {
responseMQTTST.response = response;
var camname = mqttSTopicsValues[stopic].name;
let messageReturn = JSON.stringify({
"volume": {
'max': mqttCamStatusValues[mqttSTopicsValues[stopic].name].volume.max, 'mean': mqttCamStatusValues[mqttSTopicsValues[stopic].name].volume.mean,
'ffmpeg_stat': mqttCamStatusValues[mqttSTopicsValues[stopic].name].volume.ffmpeg_stat, 'cam_stat': responseMQTTST.response, "time": new Date().toISOString()
}
});
//Wait for last FFMPEG Analysis to stop or complete then send the MQTT stopped response
setTimeout(() => {
//console.log("camname: " + camname);
mqttpublish(topic + "/" + camname, messageReturn, mqttmsgoptions, responseMQTTST.response, function (posted) { });
}, FFMPEGupdateTime * 1000 + 5000);
});
mqttpublish(stopic, JSON.stringify(responseMQTTST), mqttmsgoptions, stopic, function (response) {
});
}
//Restart Camera Stream Analysis from MQTT message
if (stopic == (clientid + "/restartCamStream") && mqttSTopicsValues[stopic].activate) {
restartCamStream(mqttSTopicsValues[stopic], function (response) {
responseMQTTST.response = response;
var camname = mqttSTopicsValues[stopic].name;
let messageReturn = JSON.stringify({
"volume": {
'max': mqttCamStatusValues[mqttSTopicsValues[stopic].name].volume.max, 'mean': mqttCamStatusValues[mqttSTopicsValues[stopic].name].volume.mean,
'ffmpeg_stat': mqttCamStatusValues[mqttSTopicsValues[stopic].name].volume.ffmpeg_stat, 'cam_stat': responseMQTTST.response, "time": new Date().toISOString()
}
});
//Wait for last FFMPEG Analysis to stop or complete then send the MQTT stopped response
setTimeout(() => {
//console.log("camname: " + camname);
mqttpublish(topic + "/" + camname, messageReturn, mqttmsgoptions, responseMQTTST.response, function (posted) { });
}, FFMPEGupdateTime * 1000 + 5000);
});
mqttpublish(stopic, JSON.stringify(responseMQTTST), mqttmsgoptions, stopic, function (response) {
});
}
//Return Latest Camera Stream Analysis from MQTT message
if (stopic == (clientid +"/requestCamStreamData") && mqttSTopicsValues[stopic].activate) {
requestCamStreamData(mqttSTopicsValues[stopic], function (response) {
responseMQTTST.response = response;
responseMQTTST.data = mqttCamStatusValues[mqttSTopicsValues[stopic].name];
console.log("Response: " + responseMQTTST);
console.log("MQTTStopic: " + mqttSTopics[stopic]);
});
mqttpublish(stopic, JSON.stringify(responseMQTTST), mqttmsgoptions, stopic, function (response) {
});
}
//-FUTURE- Update Config File with current cameras from MQTT message
if (stopic == (clientid + "/updateConfig") && mqttSTopicsValues[stopic].activate) {
updateConfig(mqttSTopicsValues[stopic], function (response) {
responseMQTTST.response = response;
});
mqttpublish(stopic, JSON.stringify(responseMQTTST), mqttmsgoptions, stopic, function (response) {
});
}
});
function mqttpublish(Ptopic,msg,options,id,callback) {
//mqtt publish only if connectted
let posted = true;
if (client.connected == true) {
client.publish(Ptopic, msg, options);
console.log((new Date().toISOString()) + ": MSG Published : " + id + ": " + Ptopic + " msg: " + msg + " at " + new Date().toISOString());
return callback(posted);
}
posted = false;
return callback(posted);
}
function mqttsubscribe(stopic, qos, callback) {
//mqtt publish only if connectted
let subscribeposted = true;
if (client.connected == true) {
client.subscribe(stopic, qos);
console.log((new Date().toISOString()) + ": MQTT Subscribed: " + stopic + " at " + new Date().toISOString());
return callback(subscribeposted);
}
subscribeposted = false;
return callback(subscribeposted);
}
//Adds the stream from MQTT message
function addCamStream(decodeMessage, callback) {
//var addCamStreamObj = { "name": decodeMessage.name, "streamURL": decodeMessage.data, "UpdateTime": 4 };
//add the camera only if it doesn't exist
var updateTimeValue = (Number.isInteger(decodeMessage.data) ? decodeMessage.data : 4);
if (!arrayCamTimeIntervals[decodeMessage.name]) {
i = i + 1;
CAMERA_DATA[i] = { "name": decodeMessage.name, "streamURL": decodeMessage.data, "UpdateTime": updateTimeValue };
console.log(CAMERA_DATA[i].name);
startAnalysis(i, CAMERA_DATA);
console.log((new Date().toISOString()) + ": Request Repy : Stream Added");
return callback('added');
}
//log failure
console.log((new Date().toISOString()) + ": Request Repy : Camera Exists stream failed to add");
return callback('failed to add');
}
//Restart the stream function
function restartCamStream(decodeMessage, callback) {
let funcCamStatus = "";
if (arrayCamTimeIntervals[decodeMessage.name]) {
stopCamStream(decodeMessage, function (response) {
if (response != "stopped") {
return callback(response);
}
});
startCamStream(decodeMessage, function (response) {
if (response != "started") {
return callback(response);
}
});//responseMQTTST.response = response;
return callback("restarted");
}
return callback("failed to restart");
}
//Stop the stream function
function stopCamStream(decodeMessage, callback) {
if (arrayCamTimeIntervals[decodeMessage.name]) {
CAMERA_FFMPEG_PROCESS[decodeMessage.name].kill();
clearInterval(arrayCamTimeIntervals[decodeMessage.name]);
console.log((new Date().toISOString()) + ": Request Repy : Camera Analysis Stopped : " + decodeMessage.name);
arrayFFMPEGComplete[decodeMessage.name] = false;
return callback('stopped');
}
console.log("Camera: " + decodeMessage.name + " not found!");
return callback('failed to stop');
}
//Request the camera stream data function. Checks if cam exist.
function requestCamStreamData(decodeMessage, callback) {
if (arrayCamTimeIntervals[decodeMessage.name]) {
return callback("cam found sending data");
}
return callback("data request failed");
}
//Start the camera stream function. Checks if cam exist and starts the stream analysis.
function startCamStream(decodeMessage, callback) {
//var addCamStreamObj = { "name": decodeMessage.name, "streamURL": decodeMessage.data, "UpdateTime": 4 };
//Start camera only if it exist
if (arrayCamTimeIntervals[decodeMessage.name]) {
//CAMERA_DATA[arrayCamTimeIntervals.indexOf(decodeMessage.name)] = { "name": decodeMessage.name, "streamURL": decodeMessage.data, "UpdateTime": 4 };
console.log((new Date().toISOString()) + ": Request Repy : " + Object.keys(arrayCamTimeIntervals).indexOf(decodeMessage.name));
console.log((new Date().toISOString()) + ": Request Repy : " + CAMERA_DATA[Object.keys(arrayCamTimeIntervals).indexOf(decodeMessage.name)].name);
startAnalysis(Object.keys(arrayCamTimeIntervals).indexOf(decodeMessage.name), CAMERA_DATA);
console.log((new Date().toISOString()) + ": Request Repy : Camera : " + Object.keys(arrayCamTimeIntervals).indexOf(decodeMessage.name) + " Stream Analysis Started.");
return callback('started'); }
//log failure
console.log((new Date().toISOString()) + ": Request Repy : Camera " + Object.keys(arrayCamTimeIntervals).indexOf(decodeMessage.name) + " Steam Analysis Failed to Start");
return callback('failed to start');
}
//-Future- will update the config.json file with current cameras
function updateCamStreamSettings(decodeMessage) {
}
console.log("Listening for Events");
//Audio Volume Detection Function
function getVolumeAnalysis(SINGLECAMERA_DATA, id, callback) {
CAMERA_FFMPEG_PROCESS[SINGLECAMERA_DATA.name] = new ffmpeg({ source: SINGLECAMERA_DATA.streamURL })
.withAudioFilter('volumedetect')
.addOption('-f', 'null')
//.addOption('-hwaccel', 'vaapi') //this is my attempt at hardware acceleration -FUTURE-
//.addOption('- hwaccel_device', '/dev/dri/renderD128') //this is my attempt at hardware acceleration -FUTURE-
.addOption('-t', SINGLECAMERA_DATA.UpdateTime) // duration of analysis
.noVideo()
.on('start', function (ffmpegCommand) {
console.log((new Date().toISOString()) + ': ffmpeg command : ' + id + ': ' + SINGLECAMERA_DATA.name);// + ' cmd: ' + ffmpegCommand);
})
.on('end', function (stdout, stderr) {
// console.log('ffmpeg ended');
// find the mean_volume in the output
clearTimeout(killprocess);
let maxVolumeRegex = stderr.match(/max_volume:\s(-\d*(\.\d+)?)/);
let meanVolumeRegex = stderr.match(/mean_volume:\s(-\d*(\.\d+)?)/);
// return the max and mean volume and timestamp
let statVolume = {};
statVolume.id = id;
//Set the callback values if max volume found
if (maxVolumeRegex) {
statVolume.max = parseFloat(maxVolumeRegex[1]);
statVolume.mean = parseFloat(meanVolumeRegex[1]);
statVolume.timestamp = new Date().toISOString();
statVolume.error = false;
statVolume.complete = true;
statVolume.status = "ffmpeg completed";
return callback(statVolume);
}
// if the stream is not available do this
if (stderr.match(/Server returned 404 Not Found/)) {
console.log((new Date().toISOString()) + ': ffmpeg reply : ffmpeg 404 error: ' + SINGLECAMERA_DATA.name);
CAMERA_FFMPEG_PROCESS[SINGLECAMERA_DATA.name].kill('SIGSTOP');
statVolume.complete = false;
statVolume.error = false;
statVolume.errmsg = 'ffmpeg 404 error: ' + SINGLECAMERA_DATA.name;
statVolume.status = "ffmpeg 404 error";
return callback(statVolume);
}
// if the stream returns a bad request do this
if (stderr.match(/Server returned 400 Bad Request/)) {
console.log((new Date().toISOString()) + ': ffmpeg reply : ffmpeg 400 error: ' + SINGLECAMERA_DATA.name);
CAMERA_FFMPEG_PROCESS[SINGLECAMERA_DATA.name].kill('SIGSTOP');
statVolume.complete = false;
statVolume.error = true;
statVolume.errmsg = 'ffmpeg 400 error: ' + SINGLECAMERA_DATA.name;
statVolume.status = "ffmpeg 400 error";
return callback(statVolume);
}
//It went to crap, catch all log message
console.log((new Date().toISOString()) + ': ffmpeg reply : Analysis Failure' + stderr);
statVolume.complete = false;
statVolume.error = true;
statVolume.errmsg = 'Unknown error: ' + SINGLECAMERA_DATA.name;
statVolume.status = "Something went really wrong";
return callback(statVolume);
})
//progress updates maybe use in the future
//.on('progress', function (progress) {
// console.log('Processing: ' + SINGLECAMERA_DATA.name + " at " + progress.percent + '% done');
//})
// Deal with Errors when running FFMPEG command or when FFMPEG stopped
.on('error', function (err, stdout, stderr) {
let statVolume = {};
statVolume.id = id;
console.log((new Date().toISOString()) + ': ffmpeg reply : ffmpeg error: ' + SINGLECAMERA_DATA.name);
//console.log('Cannot process video, kill process: ' + SINGLECAMERA_DATA.name +' error: ' + err.message);
statVolume.error = true;
statVolume.errmsg = 'ffmpeg major error: Cannot process audio for ' + SINGLECAMERA_DATA.name;
statVolume.stat = "ffmpeg major error";
CAMERA_FFMPEG_PROCESS[SINGLECAMERA_DATA.name].kill();
return callback(statVolume);
})
.saveToFile('/dev/null');
// Aways kill ffmpeg after ffmpegupdatetime + 5 seconds
var killprocess = setTimeout(function () {
let statVolume = {};
/*ffmpegAnalysis.on('error', function () {
console.log('Ffmpeg has been killed! kill process: ' + SINGLECAMERA_DATA.name + ' error: ');
statVolume.error = true;
statVolume.errmsg = 'ffmpeg process timeout ' + SINGLECAMERA_DATA.name;
statVolume.stat = "ffmpeg process timeout ";
});*/
console.log((new Date().toISOString()) + ': ffmpeg reply : Ffmpeg timeout killed: ' + SINGLECAMERA_DATA.name);
statVolume.error = true;
statVolume.errmsg = 'ffmpeg process timeout ' + SINGLECAMERA_DATA.name;
statVolume.stat = "ffmpeg process timeout ";
CAMERA_FFMPEG_PROCESS[SINGLECAMERA_DATA.name].kill();
return callback(statVolume);
}, (FFMPEGupdateTime*1000 + 5000));
}