-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv2.js
268 lines (203 loc) · 8.3 KB
/
v2.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
266
267
268
const { readdirSync } = require("fs");
const path = require("path");
const fs = require('fs');
const { execShell } = require("./utils");
const VideoConverter = require("./VideoConverter");
const Colour = require("./Colour");
const base = "C:\\Users\\luke\\Downloads\\I&E playground\\";
async function framerate(vid) {
const res = await execShell(`ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate "${vid}"`);
return eval(res);
}
async function processDirectory(dir) {
console.time(dir);
const categoryName = dir.split(" - ")[1].trim();
const inputs = readdirSync(path.join(base, dir)).filter(f => !f.endsWith("-final.mp4") && !f.endsWith("-all.mp4") && !f.endsWith("-landscape.mp4"));
const processed = [];
for (let i = 0; i < inputs.length; i++) {
let vid = inputs[i];
if(vid.indexOf(":\\") < 0){
vid = path.join(base, dir, vid);
}
// if (await VideoConverter.isPortrait(vid)) {
// console.log(`${vid} is portrait - making landscape...`);
// vid = await VideoConverter.addLandscapeBackgroundToPortraitVideo(vid, path.join(__dirname, 'bg.png'));
// }
const res = await processFile(vid, categoryName);
console.log(`Processed video to ${res}`);
processed.push(res);
};
const category = await mergeWithTitle(categoryName, ...processed);
console.log(`Merged category to ${category}`);
console.timeEnd(dir);
return category;
}
async function mergeWithTitle(title, ...videos) {
if (videos.length === 0) {
throw 'Expected one or more videos to process!';
}
const output = videos[0] + "-all.mp4";
const colour = 'fontcolor=0x282360';
let inputs = "";
let filter = "";
for (let i = 0; i < videos.length; i++) {
inputs += ` -i "${videos[i]}"`;
filter += ` [${i + 1}:v] [${i + 1}:a]`;
}
await execShell(`ffmpeg -i "${path.join(__dirname, "bg.png")}" ${inputs.trim()} -filter_complex "${filter} concat=n=${videos.length}:v=1:a=1 [v] [a];[v]setpts=PTS-STARTPTS+5/TB[v];[0:v][v]overlay[v];[a]adelay=5000|5000[a];[v]drawtext=fontfile='/Users/luke/Downloads/font.ttf':${colour}:fontsize=70:x=(w-text_w)/2:y=(h*0.35):text='${title.toUpperCase()}':enable='between(t,0,4)',drawtext=fontfile='/Users/luke/Downloads/Open_Sans/static/OpenSans/OpenSans-Regular.ttf':${colour}:fontsize=50:x=(w-text_w)/2:y=(h*0.65):text='I&E 2022':enable='between(t,0,4)'[v]" -map "[v]" -map "[a]" "${output}" -y`);
return output;
}
async function merge(...videos) {
if (videos.length === 0) {
throw 'Expected one or more videos to process!';
}
// let offset = 0;
// while(videos.length > 5){
// const data = videos.splice(offset, 5);
// const processed = await merge(...data);
// videos.splice(offset, 0, processed);
// offset++;
// if(offset >= videos.length){
// offset = 0;
// }
// }
const parsed = path.parse(videos[0]);
const output = path.join(__dirname, 'tmp', parsed.name + "-all.mp4");
let inputs = "";
let filter = "";
for (let i = 0; i < videos.length; i++) {
inputs += ` -i "${videos[i]}"`;
filter += ` [${i}:v] [${i}:a]`;
}
await execShell(`ffmpeg ${inputs.trim()} -filter_complex "${filter} concat=n=${videos.length}:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" "${output}" -y`);
return output;
}
async function processFile(file, category) {
console.time(file);
const duration = 5;
const targetFPS = 30;
const target_w = 1920;
const target_h = 1080;
const colour = 'fontcolor=0x282360';
const enabled = `enable='between(t,0,${duration - 0.1})'`;
const output = `${file}-final.mp4`;
const bg = path.join(__dirname, "bg.png");
const fileData = path.parse(file);
const parts = path.parse(file).name.replaceAll(fileData.ext, "").split(' - ');
const name = parts[0].trim().replaceAll("'", "’");
let band = '';
if (parts.length > 1) {
band = parts[1].replaceAll("'", "’");
}
const isLandscape = await VideoConverter.isLandscape(file);
//pad=iw:2*trunc(ih*${target_w}/${target_h}):(ow-iw)/2:(oh-ih)/2,
let inputs = `-i "${file}" `;
let filter = "";
let bgLabel = "[1:v]";
if(isLandscape){
Colour.writeColouredText("Video is landscape", Colour.OPTIONS.FG_CYAN);
inputs += `-loop 1 -t 5 -i "${bg}" -t 6 -f lavfi -i color=c=black:s=1920x1080:r=30`;
filter = `[2:v]${bgLabel}overlay=eof_action=pass[bg];`; //make bg black after 5s so we don't have tiny lil title card bits
bgLabel = "[bg]";
} else {
Colour.writeColouredText("Video is portrait", Colour.OPTIONS.FG_YELLOW);
inputs += `-i "${bg}"`;
}
filter += `[0:v]scale=${target_w}x${target_h}:force_original_aspect_ratio=decrease,fps=${targetFPS},setpts=PTS-STARTPTS+${duration}/TB[delayed];`; //offset video start for title card
filter += `${bgLabel}[delayed]overlay=(W-w)/2:(H-h)/2[out];` //overlay bg and video
filter += `[0:a]adelay=${duration * 1000}|${duration * 1000}[aud];` //delay our audio too
filter += `[out]drawtext=fontfile='/Users/luke/Downloads/font.ttf':${colour}:fontsize=70:x=(w-text_w)/2:y=(h*0.35):text='${name.toUpperCase()}':${enabled}`; //add participant
if (band.trim().length > 0) {
filter += `,drawtext=fontfile='/Users/luke/Downloads/Open_Sans/static/OpenSans/OpenSans-Light.ttf':${colour}:fontsize=70:x=(w-text_w)/2:y=(h)/2:text='${band}':${enabled}` //add band
}
filter += `,drawtext=fontfile='/Users/luke/Downloads/Open_Sans/static/OpenSans/OpenSans-Regular.ttf':${colour}:fontsize=50:x=(w-text_w)/2:y=(h*0.65):text='${category}':${enabled}[out]`; //add category
const cmd = `ffmpeg ${inputs} -filter_complex "${filter}" -map [out] -map [aud] -r ${targetFPS} -c:v libx264 -preset ultrafast "${output}" -y`;
await execShell(cmd);
console.timeEnd(file);
return output;
}
async function main() {
console.time("app");
const dirs = fs.readdirSync(base, { withFileTypes: true }).filter(d => d.isDirectory()).map(d => d.name);
const categories = [];
const introSrc = path.join(__dirname, "intro.mp4");
const breakSrc = path.join(__dirname, "Mid sponsor.mp4");
const sponsorSrc = path.join(__dirname, "sponsor.mp4");
let runningTime = (await VideoConverter.getDuration(introSrc)).totalSeconds;
const breakTime = (await VideoConverter.getDuration(breakSrc)).totalSeconds;
const sponsorTime = (await VideoConverter.getDuration(sponsorSrc)).totalSeconds;
categories.push(introSrc, sponsorSrc);
const runningOrder = [{
name: "Intro",
path: introSrc,
duration: runningTime,
startTime: 0
}, {
name: "Ad",
path: sponsorSrc,
duration: sponsorTime,
startTime: runningTime
}];
let timeSinceBreak = 0;
console.time("Process categories");
for (let i = 0; i < dirs.length; i++) {
const res = await processDirectory(dirs[i]);
categories.push(res);
const duration = await VideoConverter.getDuration(res);
runningOrder.push({
name: dirs[i],
path: res,
duration: duration,
startTime: runningTime
});
runningTime += duration.totalSeconds;
timeSinceBreak += duration.totalSeconds;
if (timeSinceBreak >= 3600 && dirs.length - i > 2 && dirs[i].substring(0, 1) != dirs[i + 1].substring(0, 1)) {
categories.push(breakSrc);
runningOrder.push({
name: 'Break',
path: breakSrc,
duration: breakTime,
startTime: runningTime
});
console.log(`${timeSinceBreak} seconds since break - adding break at ${runningTime} for ${breakTime + sponsorTime} seconds`);
timeSinceBreak = 0;
runningTime += breakTime + sponsorTime;
}
}
console.timeEnd("Process categories");
categories.push(sponsorSrc);
runningOrder.push({
name: "Outro",
path: sponsorSrc,
duration: sponsorTime,
startTime: runningTime
});
fs.writeFileSync(path.join(__dirname, "tmp", "running-order.json"), JSON.stringify(runningOrder));
// const runningOrder = require(path.join(__dirname, "tmp", "running-order.json"));
// const categories = runningOrder.map(r => {
// if(r.name == "Intro"){
// return path.join(__dirname, "intro.mp4");
// }
// if(r.name == "Break"){
// return path.join(__dirname, "Mid sponsor.mp4");
// }
// if(r.name == "Outro"){
// return path.join(__dirname, "sponsor.mp4");
// }
// if(r.name.indexOf(".mov-final") < 0){
// return r.name.replace("-final", ".mp4-final");
// } else {
// return r.name.replace("-final", "-final.mp4");
// }
// return r.name;
// });
// categories.splice(1, 0, path.join(__dirname, "sponsor.mp4"));
const finals = await merge(...categories);
console.log(`Final - ${finals}`);
console.timeEnd("app");
}
main().catch(e => {
console.log(e);
console.timeEnd("app");
});