-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream-video.js
90 lines (72 loc) · 3.16 KB
/
stream-video.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
(function () {
'use strict';
const spawn = require('child_process').spawn;
const fs = require('fs');
const rimraf = require('rimraf');
const glob = require('glob');
const ffmpeg = require('fluent-ffmpeg');
const newFolder = require('./new-folder');
let singleton = true;
// const settings = require('./video-settings');
// spawnFfmpeg(settings[0]);
function getFfmpeg(settings, stream) {
return ffmpeg().input(stream)
.fps(settings.outputFps)
.save(`${settings.outputVideoFolder}/${new Date().toLocaleString().split('-').join('').split(':').join('')}.mp4`)
.duration(settings.outputVideoDuration)
.on('SIGINT', () => console.log('Ffmpeg Control C'))
.on('error', (err) => {
console.log('An error occurred: ' + err.message);
})
.on('end', () => {
getFfmpeg(settings, stream);
});
}
function recordVideoFromStream(settings, stream) {
newFolder(settings.outputVideoFolder);
const command = getFfmpeg(settings, stream);
}
function spawnFfmpeg(settings) {
if (!singleton) return;
if ((new Date().getMinutes())%2 !== 0) return;
newFolder(settings.outputImageFolder);
newFolder(settings.outputVideoFolder);
const current = new Date();
const oneHourAgo = new Date(current - 60*60*1000);
let input = getInputFilePattern(settings, oneHourAgo);
let output = getOutputFile(settings, oneHourAgo);
glob(input.replace('%d', '*'), (err, files) => {
if (!files || !files.length) {
input = getInputFilePattern(settings, current);
output = getOutputFile(settings, current);
}
const args = ['-y', '-framerate', settings.outputFps, '-i', input, output];
const ffmpeg = spawn('ffmpeg', args);
singleton = false;
console.log('Spawning ffmpeg ' + args.join(' '));
ffmpeg.on('exit', (code) => {
singleton = true;
console.log(`Child process exited with code ${code}`);
if (!code) {
console.log(`Deleting these ${input.replace('%d', '*')} files`);
rimraf(input.replace('%d', '*'), (err) => err && console.log(err));
}
});
ffmpeg.on('error', (err) => err && console.log(err));
ffmpeg.stderr.on('data', function (data) {
console.log('' + data);
});
return ffmpeg;
});
}
function getInputFilePattern(settings, date) {
return `${settings.outputImageFolder}/${date.toLocaleDateString().split('-').join('')}-${date.getHours()}-%d.jpg`;
}
function getOutputFile(settings, date) {
return `${settings.outputVideoFolder}/${date.toLocaleDateString().split('-').join('')}-${date.getHours()}-${new Date().toLocaleTimeString().split(':').join('')}.mp4`;
}
module.exports = {
spawnFfmpeg: spawnFfmpeg,
recordVideoFromStream: recordVideoFromStream
};
})();