-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
265 lines (191 loc) · 8.17 KB
/
index.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
const debug = require('debug')('stitcheroo');
const child_process = require('child_process');
const fs = require('fs');
const ffprobe = require('ffprobe-static');
const ffmpeg = require('ffmpeg-static');
const shortid = require('shortid').generate;
const boxjam = require('boxjam');
const os = require('os');
const path = require('path')
// generate a temporary filename in the system temporary directory
// given the file's extension
const tmp = (extension) => {
return path.join(os.tmpdir(), `${shortid()}${extension}`)
}
// Delete created files when they've been loaded into memory
// or if the program crashes.
function cleanUp(filePath){
fs.unlink(filePath, err => {
if(err){
debug(`Unable to delete file ${filePath}:`, err);
}
});
}
function probeVideo(filePath){
return new Promise( (resolve, reject) => {
const probe = child_process.spawn(ffprobe.path, ['-v', 'error', '-show_entries', 'stream=width,height', '-of', 'default=noprint_wrappers=1:nokey=1', filePath])
probe.stdout.on('data', (data) => {
data = data.toString('utf8');
const textBasedDimensions = data.split('\n');
const result = {
width : Number(textBasedDimensions[0]),
height : Number(textBasedDimensions[1]),
path : filePath
};
resolve(result);
});
probe.stderr.on('data', (data) => {
debug(`FFProbe stderr: ${data}`);
});
probe.on('close', (code) => {
debug(`FFProbe exited with code ${code}`);
if(code === 1){
reject('FFProbe did not behave as expected.');
}
});
});
}
function stitchVideo(videos, container, margin, shouldCenter, returnAsFile, pan, reverb){
return new Promise( (resolve, reject) => {
const OUTPUT_FILE_NAME = tmp('.mp4')
const boxes = boxjam(videos, container, margin, shouldCenter);
// ensure reverb parameters are valid, if supplied
if (reverb) {
if (!['none', 'smallroom', 'largeroom', 'hall', 'church'].includes(reverb.type)) {
reverb.type = 'none'
}
if (typeof reverb.mix !== 'number' || reverb.mix < 0 || reverb.mix > 1) {
reverb.mix = 0.1
}
}
let FILTER = `"[0:v]scale=${container.width}:${container.height}[bg]; `
// Scale inputs
for(let i = 0; i < videos.length; i += 1){
FILTER += `[${i + 1}:v]scale=${boxes[i].width}:${boxes[i].height},setpts=PTS-STARTPTS[fg${i + 1}]; `
}
FILTER += `[bg][fg1]overlay=${boxes[0].x}:${boxes[0].y}:shortest=1[ol0]; `
// Overlay inputs
let lastKey = "ol0";
for(let j = 1; j < videos.length; j += 1){
const thisKey = `ol${shortid()}`;
FILTER += `[${lastKey}][fg${j + 1}]overlay=${boxes[j].x}:${boxes[j].y},format=yuv420p[${thisKey}]${ j === videos.length -1 ? `"` : `;` }`
lastKey = thisKey;
}
// Construct audio arguments
let AUDIO_FLAGS
if (pan) {
// pan audio left or right depending on its position in the output video
// [0]stereotools=mpan=-0.9 [x0]; [1]stereotools=mpan=0.9 [x1]; [x0] [x1] amix=inputs=2
AUDIO_FLAGS = videos.map((i, idx) => {
const n = idx + 1
// calculate stereo pan from where the middle of the video overlay
// pan goes from -1 (left) to 0 (centre) to 1 (right)
const pan = (2 * ((boxes[idx].x + boxes[idx].width/2) / container.width) - 1).toFixed(1)
return `[${n}]stereotools=mpan=${pan} [x${n}];`
}).join('');
const MIX_FLAGS = videos.map((i, idx) => {
const n = idx + 1
return `[x${n}]`
}).join(' ')
AUDIO_FLAGS += MIX_FLAGS
} else {
// no audio panning, simply mix each video's audio together
AUDIO_FLAGS = videos.map((i, idx) => { return `[${idx+1}]` }).join('');
}
AUDIO_FLAGS += ` amix=inputs=${videos.length}`
// additional audio processing if reverb is selected
let reverbpath
if (reverb && reverb.type !== 'none' && reverb.mix > 0) {
reverbpath = path.join(__dirname, `${reverb.type}.wav`);
const mix = reverb.mix * 10
const weights = `${10 - mix} ${mix}`
AUDIO_FLAGS += '[mix]; '; // name the previous audio stream as "mix"
AUDIO_FLAGS += '[mix] asplit [mix1][mix2];'; // split the mix into two: mix1 for IR processing, mix2 for final mix
AUDIO_FLAGS += `[mix1] [${videos.length + 1}] afir=dry=10:wet=10 [reverb]; `; // apply IR to the mix1
AUDIO_FLAGS += `[mix2] [reverb] amix=inputs=2:weights=${weights}`; // blend mix2 + reverb
}
AUDIO_FLAGS += '"'
let stitchArguments = ['-loop', 1, '-i', `${__dirname}/vid_back.png`, ];
const inputArguments = [];
for(let v = 0; v < boxes.length; v += 1){
inputArguments.push('-i');
inputArguments.push(`${boxes[v].path}`);
}
// if we are doing reverb, we need to load the Impulse Response wav file as another inputs
if (reverbpath) {
inputArguments.push('-i');
inputArguments.push(`${reverbpath}`);
}
// Construct the video + audio filter arguments and output paths
const filterArguments = ['-filter_complex', FILTER, '-map', `"[${lastKey}]"`, '-movflags', '+faststart ', '-filter_complex', `"${AUDIO_FLAGS}`, '-c:a', 'aac', '-threads', 8, OUTPUT_FILE_NAME, '-y'];
// And combine all of the arguments
stitchArguments = stitchArguments.concat(inputArguments, filterArguments);
// Then execute!
const stitch = child_process.spawn(ffmpeg, stitchArguments, { shell: true });
stitch.stdout.on('data', (data) => {
debug(data);
});
stitch.stderr.on('data', (data) => {
debug(`stderr: ${data}`);
});
stitch.on('close', (code) => {
debug(`child process exited with code ${code}`);
if(code === 0){
if(returnAsFile){
resolve(OUTPUT_FILE_NAME);
} else {
fs.readFile(`${OUTPUT_FILE_NAME}`, (err, data) => {
cleanUp(`${OUTPUT_FILE_NAME}`);
if(err){
debug('Filesystem read err:'. err);
reject(err);
} else {
resolve(data);
}
});
}
} else {
debug(`FFMPEG exited and was not happy. Error code: ${code}`);
reject();
cleanUp(`${OUTPUT_FILE_NAME}`);
}
});
});
}
module.exports = (videos, userOptions = {}) => {
if(userOptions.dimensions){
if(!userOptions.dimensions.width || !userOptions.dimensions.height){
return Promise.reject(`Invalid dimension values passed. Both "width" and "height" must be passed. Function received ${JSON.stringify(userOptions.dimensions)}`)
}
}
const options = {
dimensions : {
width: 1920,
height: 1080
},
margin: 20,
center: true,
returnAsFile: false,
pan: true,
reverb: {
type: 'none',
mix: 0.1
}
};
Object.keys(userOptions).forEach(key => {
options[key] = userOptions[key];
});
debug(`Options passed by user: ${JSON.stringify(userOptions)}`);
debug(`Options passed for rendering: ${JSON.stringify(options)}`);
const inputFiles = videos.map(filePath => probeVideo(filePath));
return Promise.all(inputFiles)
.then(results => {
// return results;
return stitchVideo(results, options.dimensions, options.margin, options.center, options.returnAsFile, options.pan, options.reverb);
})
.catch(err => {
debug('err:', err);
throw err;
})
;
}