-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathae.js
130 lines (106 loc) · 4.83 KB
/
ae.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
const { spawn, exec } = require("child_process");
const path = require("path");
const fs = require("fs");
function render(AEPath, config, progress) {
return new Promise((resolve, reject) => {
let macRender = (process.platform === "darwin") || !!config.useExec;
delete config.useExec;
let renderLogFile = config.project + `_${Date.now()}_logs.txt`;
if (macRender) {
config.log = renderLogFile;
}
let renderConfig = [];
for (const [key, value] of Object.entries(config)) {
let val = value.toString();
if (["project", "comp", "OMtemplate", "RStemplate", "output", "log"].includes(key)) {
val = `"${value}"`;
}
if (["reuse", "continueOnMissingFootage"].includes(key)) {
if (value) {
renderConfig.push(`-${key}`);
}
} else {
renderConfig.push(`-${key}`);
renderConfig.push(val);
}
}
const progressRegex = /([\d]{1,2}:[\d]{2}:[\d]{2}:[\d]{2})\s+(\(\d+\))/gi;
const durationRegex = /Duration:\s+([\d]{1,2}:[\d]{2}:[\d]{2}:[\d]{2})/gi;
const startRegex = /Start:\s+([\d]{1,2}:[\d]{2}:[\d]{2}:[\d]{2})/gi;
const errorRegex = /aerender Error:\s*(.*)$/gis;
const seconds = (string) => string.split(':')
.map((e, i) => (i < 3) ? +e * Math.pow(60, 2 - i) : +e * 10e-6)
.reduce((acc, val) => acc + val);
let projectDuration = null;
let currentProgress = null;
let previousProgress = undefined;
let projectStart = null;
let matchError = null;
if (macRender) {
let watchLog = null;
exec(`"${path.join(AEPath, 'aerender')}" ${renderConfig.join(' ')}`, (error, stdout, stderr) => {
clearInterval(watchLog);
resolve({ error, stdout, stderr });
});
const parseProgress = (fullLog) => {
let lastLineArr = fullLog.split("\r");
let lastLine = lastLineArr[lastLineArr.length - 2];
if (projectStart == null) {
let matchStart = startRegex.exec(fullLog);
if (matchStart) {
projectStart = seconds(matchStart[1]);
}
}
if (projectDuration == null) {
let matchDuration = durationRegex.exec(fullLog);
if (matchDuration) {
projectDuration = seconds(matchDuration[1]);
}
}
const matchProgress = !isNaN(parseInt(projectDuration)) ? progressRegex.exec(lastLine) : null;
if (matchProgress) {
currentProgress = Math.ceil((seconds(matchProgress[1]) - projectStart) * 100 / projectDuration);
if (previousProgress !== currentProgress) {
progress(currentProgress);
previousProgress = currentProgress;
}
}
}
watchLog = setInterval(() => {
if (fs.existsSync(renderLogFile)) {
parseProgress(fs.readFileSync(renderLogFile).toString());
}
}, 1000);
} else {
let ae = spawn(path.join(AEPath, 'aerender'), renderConfig);
const parseProgress = (data) => {
const string = data.toString('utf8').replace(/;/g, ':');
const matchStart = isNaN(parseInt(projectStart)) ? startRegex.exec(string) : false;
const matchDuration = isNaN(parseInt(projectDuration)) ? durationRegex.exec(string) : false;
const matchProgress = !isNaN(parseInt(projectDuration)) ? progressRegex.exec(string) : null;
projectDuration = (matchDuration) ? seconds(matchDuration[1]) : projectDuration;
projectStart = (matchStart) ? seconds(matchStart[1]) : projectStart;
if (progress && matchProgress) {
currentProgress = Math.ceil((seconds(matchProgress[1]) - projectStart) * 100 / projectDuration);
if (previousProgress !== currentProgress) {
progress(currentProgress);
previousProgress = currentProgress;
}
}
matchError = errorRegex.exec(string);
return data;
}
ae.stdout.on('data', function (data) {
parseProgress(data.toString('utf8'));
});
ae.on('close', function (code) {
if (matchError !== null) {
reject(matchError);
} else {
resolve();
}
});
}
});
}
module.exports = render;