forked from imonology/snapshot_with_ffmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.js
127 lines (110 loc) · 2.83 KB
/
snapshot.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
var child_process = require("child_process");
var fs = require("fs");
var path = require("path");
var del_old_snapshot = function (filename) {
fs.stat(path.resolve('./', filename), function (err, stat) {
if (err) return;
var mtime = new Date(stat.mtime);
if (mtime < new Date(new Date() - 1*60000)) {
try {
fs.unlink(path.resolve('./', filename));
} catch (e) {
// LOG.error(err);
}
} else {
}
});
}
fs.readdir('./', function (err, files) {
if (err) {
return;
}
for (var i in files) {
var filename = files[i].slice(0);
if (filename.match(/.*png$/i)) {
del_old_snapshot(filename);
}
}
});
function snapshot () {
this.init();
return this;
};
snapshot.prototype.init = function () {
var self = this;
this.old_snapshot = "";
fs.watch("./", {presistent: true, recursive: true}, function (event, filename) {
if (event === 'rename') {
return;
}
if (filename.match(/.*png$/i)) {
self.keepalive();
if (self.old_snapshot === "") {
self.old_snapshot = (path.resolve('./', filename));
} else if (self.old_snapshot !== path.resolve('./', filename)) {
try {
fs.rename(self.old_snapshot, "./snapshot.jpg");
} catch (e) {
console.log(e);
}
self.old_snapshot = path.resolve('./', filename);
}
}
});
try {
this.ffmpeg_snapshot = child_process.spawn('/usr/local/bin/ffmpeg', [
'-y',
'-loglevel', 'fatal',
'-codec', 'h264_mmal',
'-i', 'rtsp://localhost:8554/h264',
'-r', '2',
// '-vf', 'fps=1',
'-vf', 'drawtext=fontfile=/usr/share/fonts/truetype/wqy/wqy-microhei.ttc : text=%{localtime\}: fontcolor=yellow@1: x=10: y=10',
path.resolve('./', 'snapshot%01d.png')
]);
this.keep_rtsp = setInterval(function () {
console.log('rtsp is dead, rrrrrrrrrrrrrrrreconnecting ... ');
self.reconnect_rtsp();
}, 5000);
} catch (err) {
console.log(err);
}
this.ffmpeg_snapshot.stderr.on('error', function (data) {
console.log(data.toString());
});
this.ffmpeg_snapshot.stderr.on('data', function (data) {
console.log(data.toString());
});
this.ffmpeg_snapshot.on('close', function(code) {
console.log(code);
});
}
snapshot.prototype.keepalive = function () {
var self = this;
if (typeof(this.keep_rtsp) !== "undefined" || typeof(this.keep_rtsp) !== {}) {
clearInterval(this.keep_rtsp);
delete this.keep_rtsp;
}
this.keep_rtsp = setInterval(function () {
console.log('rtsp is dead, rrrrrrrrrrrrrrrreconnecting ... ');
self.reconnect_rtsp();
}, 5000);
}
snapshot.prototype.reconnect_rtsp = function () {
if (typeof(this.keep_rtsp) !== "undefined" || typeof(this.keep_rtsp) !== {}) {
clearInterval(this.keep_rtsp);
delete this.keep_rtsp;
}
try {
this.ffmpeg_snapshot.kill('SIGHUP');
} catch (e) {
if (e) {
console.log(e);
self.reconnect_rtsp();
return;
}
}
delete this.ffmpeg_snapshot;
this.init();
}
var ss = new snapshot();