-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample.ts
112 lines (101 loc) · 2.84 KB
/
example.ts
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
/* eslint-disable no-console */
import readline from 'readline';
import Recorder, { RecorderEvents } from './src/recorder';
const log = (event: string) => (...args: unknown[]) => {
console.log(new Date().toString());
console.log(`Event "${event}": `, ...args);
console.log();
};
function logProgress(...args: unknown[]) {
return log(RecorderEvents.PROGRESS)(...args);
}
readline.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
}
try {
const {
SOURCE,
IP,
TITLE,
SEGMENT_TIME,
THRESHOLD,
FILE_PATTERN,
NO_AUDIO,
DESTINATION,
SHOW_PROGRESS,
PLAYLIST_NAME,
FFMPEG_BINARY,
} = process.env;
if (!DESTINATION || (!SOURCE && !IP) || (SOURCE && IP)) {
console.warn('Error: Please specify SOURCE or IP + DESTINATION.');
process.exit(1);
}
const source = SOURCE || `rtsp://${IP}:554/user=admin_password=tlJwpbo6_channel=1_stream=1.sdp?real_stream`;
const title = TITLE || 'Example cam';
const safeTitle = title
.replace(/[:]+/ug, '_')
.replace(/_+/ug, '_');
const segmentTime = SEGMENT_TIME || '10m';
const dirSizeThreshold = THRESHOLD || '500M';
const noAudio = NO_AUDIO === 'true' ? true : false;
const filePattern = FILE_PATTERN || `${safeTitle}-%Y.%m.%d/%H.%M.%S`;
const playlistName = PLAYLIST_NAME || safeTitle;
const recorder = new Recorder(source, DESTINATION, {
title,
segmentTime,
filePattern,
playlistName,
dirSizeThreshold,
noAudio,
ffmpegBinary: FFMPEG_BINARY,
});
if (SHOW_PROGRESS) {
recorder.on(RecorderEvents.PROGRESS, logProgress);
}
else {
recorder.on(RecorderEvents.PROGRESS, logProgress)
.on(RecorderEvents.STARTED, () => {
recorder.removeListener(RecorderEvents.PROGRESS, logProgress);
})
.on(RecorderEvents.STOP, () => {
recorder.on(RecorderEvents.PROGRESS, logProgress);
});
}
recorder
.on(RecorderEvents.START, log(RecorderEvents.START))
.on(RecorderEvents.STARTED, log(RecorderEvents.STARTED))
.on(RecorderEvents.STOP, log(RecorderEvents.STOP))
.on(RecorderEvents.STOPPED, log(RecorderEvents.STOPPED))
.on(RecorderEvents.ERROR, log(RecorderEvents.ERROR))
.on(RecorderEvents.FILE_CREATED, log(RecorderEvents.FILE_CREATED))
.on(RecorderEvents.SPACE_FULL, log(RecorderEvents.SPACE_FULL))
.start();
process.stdin.on('keypress', (_, key) => {
if (key.ctrl && key.name === 'c') {
if (recorder.isRecording()) {
recorder
.on(RecorderEvents.STOPPED, () => {
setTimeout(() => {
console.log('Gracefully stopped.');
process.exit();
}, 2000);
})
.stop();
} else {
process.exit();
}
} else if (key.name === 'space') {
if (recorder.isRecording()) {
recorder.stop();
} else {
recorder.start();
}
}
});
console.log('Press "space" to start/stop recording, "ctrl + c" to stop a process.');
console.log();
} catch (err) {
console.error(err);
process.exit(1);
}