-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo-handler.js
77 lines (69 loc) · 1.78 KB
/
video-handler.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
const spawn = require('child_process').spawn;
const timecut = require('timecut');
const buildVideo = async (config) => {
const defaultConfig = {
url: 'http://localhost:5000',
viewport: {
width: 1280,
height: 720
},
selector: 'body',
fps: 30,
duration: 2.23,
output: 'video.mp4',
quiet: true,
inputAudio: 'piloto.mp3',
finalOutput: 'final.mp4',
convert: true,
}
console.log('Starting recording...')
const recordConfig = {...defaultConfig, ...config}
const loop = await timecut(recordConfig)
console.log('Recording finished')
if(!recordConfig.convert) {
console.log('Done');
return recordConfig.output;
}
const finalVideo = await loopVideoToMp3(recordConfig)
console.log('Done');
return recordConfig.finalOutput
}
const loopVideoToMp3 = async (config) => {
const ffmpegArgs = [
'-stream_loop',
'-1',
'-i',
`${__dirname}/${config.output}`,
'-i',
`${__dirname}/${config.inputAudio}`,
'-shortest',
'-map',
'0:v:0',
'-map',
'1:a:0',
'-y',
`${__dirname}/${config.finalOutput}`,
];
console.log('Starting video loop for audio duration...')
const convertProcess = spawn('ffmpeg', ffmpegArgs);
convertProcess.stderr.setEncoding('utf8');
convertProcess.stderr.on('data', (data) => {
if(!config.quiet) {
console.log(data);
}
});
return new Promise((resolve, reject) => {
convertProcess.on('close', function () {
resolve();
});
convertProcess.on('error', function (err) {
processError = err;
reject(err);
});
convertProcess.stdin.on('error', function (err) {
processError = err;
reject(err);
});
});
}
module.exports = buildVideo