-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
61 lines (52 loc) · 1.52 KB
/
server.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
// ffmpeg -i $1.ts -c:v libx264 -preset medium -tune film -crf 23 -strict experimental -c:a aac -b:a 192k output.mp4
'use strict';
const fs = require('fs'),
INPUT_DIR = process.env.INPUT_DIR || '.',
OUTPUT_DIR = process.env.OUTPUT_DIR || '.',
spawn = require('child_process').spawn,
redis = require('redis'),
_ = require('lodash');
const redisClient = redis.createClient();
function convert(filename) {
let newFilename = filename.split('.')[0] + '.mp4';
let ffmpeg = spawn('ffmpeg', [
'-i',
`${INPUT_DIR}/${filename}`,
'-c:v',
'libx264',
'-preset',
'medium',
'-tune',
'film',
'-crf',
'23',
'-strict',
'experimental',
'-c:a',
'aac',
'-b:a',
'192k',
`${OUTPUT_DIR}/${newFilename}`
]);
ffmpeg.on('data', (code) => {
console.log(code);
});
ffmpeg.on('close', (code) => {
console.log(`child process exited with code ${code}`);
if (code !== 1) {
redisClient.publish('videofragment', `${newFilename}`);
fs.createReadStream(`${OUTPUT_DIR}/${newFilename}`)
.pipe(fs.createWriteStream(`${OUTPUT_DIR}/current.mp4`));
}
});
}
var fns = {};
fs.watch(INPUT_DIR, {}, (eventType, filename) => {
if (filename.indexOf('.ts') > -1) {
if (!fns.hasOwnProperty(filename)) {
fns[filename] = _.debounce(convert, 1500);
} else {
fns[filename](filename);
}
}
});