-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.js
268 lines (257 loc) · 6.42 KB
/
record.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
const fs = require("fs");
const child_process = require("child_process");
const express = require("express");
const util = require('util');
const path = require('path');
const config = require("./config");
const exists = util.promisify(fs.exists);
const appendFile = util.promisify(fs.appendFile);
const mkdir = util.promisify(fs.mkdir);
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const readdir = util.promisify(fs.readdir);
const stat = util.promisify(fs.stat);
const unlink = util.promisify(fs.unlink);
const Gpio = require('onoff').Gpio;
let status = 0; //0 - recording stopped, 1- start recording, 2 - recording in progress
let proc;
let videoInput = config.videoInput;
let blinkInterval = 1000;
logger("Start script");
async function logger(text) {
if (config.enableLog) {
const now = new Date();
const time = `${now.getDate().toString().padStart(2, 0)}-${(now.getMonth() + 1).toString().padStart(2, 0)}-${now.getFullYear()} ${now.getHours().toString().padStart(2, 0)}:${now.getMinutes().toString().padStart(2, 0)}:${now.getSeconds().toString().padStart(2, 0)}`;
await appendFile("log.txt", `${time} ${text}\n`);
console.log(text);
}
}
async function record() {
if (videoInput === "" || videoInput === undefined || !await exists(videoInput)) {
logger("Video input not exists");
return;
}
logger("I'm starting a recording attempt");
if (!await exists(config.videos)) {
try {
await mkdir(config.videos, {
recursive: true
});
} catch (err) {
logger("MKDIR -> " + err);
}
}
let filename;
if (await exists("count")) {
try {
filename = parseInt(await readFile("count", "utf8"));
if (isNaN(filename)) {
filename = 0;
}
} catch (err) {
filename = 0;
}
} else {
filename = 0;
}
await writeFile("count", filename + 1);
filename += ".avi";
status = 1;
proc = child_process.spawn(config.ffmpeg, ["-loglevel", "error", "-y", "-f", "alsa", "-ac", "1", "-i", config.audioInput, "-i", videoInput, "-c:v", "copy", "-c:a", "aac", "-r", "30", `${config.videos}/${filename}`], { stdio: ["ignore", "ignore", "pipe"] });
proc.stderr.on("data", (data) => {
logger("FFMPEG -> " + data);
});
proc.on("close", () => {
logger("Recording stopped");
status = 0;
child_process.spawn("sync");
proc = null;
clearInterval(timer);
});
const timer = setInterval(async () => {
if (await exists(`${config.videos}/${filename}`)) {
status = 2;
logger("Recording in progress");
clearInterval(timer);
}
}, 2000);
}
async function getFiles() {
let files = [];
if (await exists(config.videos)) {
const list = await readdir(config.videos);
for (const el of list) {
const stats = await stat(path.join(config.videos, el));
if (stats.isFile()) {
files.push(el);
}
}
}
return files;
}
const app = express();
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.get("/status", (req, res) => {
res.send(status.toString());
});
app.get("/files", async (req, res) => {
const files = await getFiles();
res.send({ files });
});
app.get("/files/:file", async (req, res) => {
const fileName = path.join(config.videos, req.params.file);
if (await exists(fileName)) {
res.download(fileName);
logger(`Loading file ${fileName}`);
} else {
res.sendStatus(404);
logger(`Not found file ${fileName}`);
}
});
app.get("/deleteFile/:file", async (req, res) => {
const fileName = path.join(config.videos, req.params.file);
if (req.params.file && await exists(fileName)) {
try {
await unlink(fileName);
logger(`File deleted ${fileName}`);
} catch (err) {
const files = await getFiles();
res.status(500).send({ files });
}
const files = await getFiles();
res.status(200).send({ files });
} else {
const files = await getFiles();
res.status(404).send({ files });
}
});
app.get("/startRecord", (req, res) => {
if (proc) {
res.sendStatus(500);
} else {
record();
res.sendStatus(200);
}
});
app.get("/stopRecord", (req, res) => {
logger("Attempt to stop recording");
if (proc) {
try {
proc.kill('SIGTERM');
res.sendStatus(200);
} catch (err) {
logger(err);
res.sendStatus(500);
}
} else {
res.sendStatus(500);
}
});
app.get("/videoInput", (req, res) => {
res.send(videoInput);
});
function searchVideoInput() {
const search = () => {
return new Promise(async (resolve, reject) => {
let count = 0;
let listDev = await readdir("/dev");
listDev = listDev.filter(el => /video\d+/.test(el));
if (!listDev.length) {
reject("Not found video input");
}
listDev.forEach(el => {
const pathVideo = path.join("/dev", el);
let test = child_process.spawn(config.ffprobe, [pathVideo]);
test.on("close", (code) => {
if (!code) {
resolve(pathVideo);
}
count++;
if (count >= listDev.length) {
reject("Not found video input");
}
});
});
});
};
const recursiveSearch = async () => {
try {
videoInput = await search();
} catch (err) {
logger(err);
videoInput = undefined;
}
if (videoInput === "" || videoInput === undefined) {
setTimeout(() => {
recursiveSearch();
}, 10000);
} else {
if (config.autoRecord) {
record();
}
}
};
recursiveSearch();
}
function start() {
if (videoInput == "auto") {
searchVideoInput();
}
app.listen(80);
if (config.autoRecord && videoInput != "auto") {
record();
}
if (config.enableGPIOcontrol) {
const button = new Gpio(config.buttonPin, 'in', 'both');
checkPressButton(button, 0);
const ledIndicator = new Gpio(config.ledIndicatorPin, 'out');
blink(ledIndicator, 1);
}
}
function blink(ledIndicator, ledIndicatorValue) {
ledIndicatorValue = ledIndicatorValue ^ 1;
setTimeout(() => {
ledIndicator.write(ledIndicatorValue, () => {
switch (status) {
case 0:
blinkInterval = 1000;
break;
case 1:
blinkInterval = 50;
break;
case 2:
blinkInterval = 200;
break;
default: blinkInterval = 1000;
}
blink(ledIndicator, ledIndicatorValue);
});
}, blinkInterval);
}
function checkPressButton(button, buttonValue) {
setTimeout(() => {
button.read((err, value) => {
if (!err) {
if (buttonValue != value) {
if (value == 1) {
if (proc) {
logger("Attempt to stop recording");
try {
proc.kill('SIGTERM');
} catch (err) {
logger(err);
}
} else {
record();
}
}
buttonValue = value;
}
}
checkPressButton(button, buttonValue);
});
}, 100);
}
start();