polotno-node 2.6.8 → 2.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -10
- package/dist/assets/index-db239b7c.js +229 -0
- package/dist/index.html +1 -1
- package/index.js +14 -8
- package/package.json +6 -5
- package/video-parallel.js +258 -0
- package/video.js +6 -2
- package/dist/assets/index-a7c43fcd.js +0 -228
package/dist/index.html
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
}
|
|
9
9
|
</style>
|
|
10
10
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
11
|
-
<script type="module" crossorigin src="./assets/index-
|
|
11
|
+
<script type="module" crossorigin src="./assets/index-db239b7c.js"></script>
|
|
12
12
|
</head>
|
|
13
13
|
<body>
|
|
14
14
|
<div id="root"></div>
|
package/index.js
CHANGED
|
@@ -49,13 +49,8 @@ const minimal_args = [
|
|
|
49
49
|
'--use-mock-keychain',
|
|
50
50
|
];
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
url,
|
|
55
|
-
useParallelPages,
|
|
56
|
-
browserArgs = [],
|
|
57
|
-
} = {}) => {
|
|
58
|
-
const browser = await puppeteer.launch({
|
|
52
|
+
async function createBrowser({ browserArgs = [], ...rest } = {}) {
|
|
53
|
+
return puppeteer.launch({
|
|
59
54
|
args: [
|
|
60
55
|
...chrome.args,
|
|
61
56
|
...minimal_args,
|
|
@@ -74,10 +69,21 @@ module.exports.createInstance = async ({
|
|
|
74
69
|
executablePath:
|
|
75
70
|
isMacOs || isWindows ? undefined : await chrome.executablePath(),
|
|
76
71
|
ignoreHTTPSErrors: true,
|
|
72
|
+
...rest,
|
|
77
73
|
});
|
|
74
|
+
}
|
|
78
75
|
|
|
76
|
+
module.exports.createBrowser = createBrowser;
|
|
77
|
+
|
|
78
|
+
module.exports.createInstance = async ({
|
|
79
|
+
key,
|
|
80
|
+
url,
|
|
81
|
+
useParallelPages,
|
|
82
|
+
browserArgs = [],
|
|
83
|
+
browser,
|
|
84
|
+
} = {}) => {
|
|
79
85
|
return await createInstance({
|
|
80
|
-
browser,
|
|
86
|
+
browser: browser || (await createBrowser({ browserArgs })),
|
|
81
87
|
key,
|
|
82
88
|
url,
|
|
83
89
|
useParallelPages,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "polotno-node",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.1",
|
|
4
4
|
"description": "Polotno workflow from NodeJS",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"index.js",
|
|
14
14
|
"instance.js",
|
|
15
15
|
"video.js",
|
|
16
|
+
"video-parallel.js",
|
|
16
17
|
"README.md"
|
|
17
18
|
],
|
|
18
19
|
"browserslist": "> 0.5%, last 2 versions, not dead",
|
|
@@ -26,7 +27,7 @@
|
|
|
26
27
|
"gifencoder": "^2.0.1",
|
|
27
28
|
"pixelmatch": "^5.3.0",
|
|
28
29
|
"pngjs": "^7.0.0",
|
|
29
|
-
"polotno": "^2.
|
|
30
|
+
"polotno": "^2.1.1",
|
|
30
31
|
"react": "^18.2.0",
|
|
31
32
|
"react-dom": "^18.2.0",
|
|
32
33
|
"vite": "^4.5.0"
|
|
@@ -34,9 +35,9 @@
|
|
|
34
35
|
"author": "Anton Lavrenov",
|
|
35
36
|
"license": "MIT",
|
|
36
37
|
"dependencies": {
|
|
37
|
-
"@sparticuz/chromium": "
|
|
38
|
-
"puppeteer": "^
|
|
39
|
-
"puppeteer-core": "^
|
|
38
|
+
"@sparticuz/chromium": "118.0.0",
|
|
39
|
+
"puppeteer": "^20.9.0",
|
|
40
|
+
"puppeteer-core": "^20.9.0"
|
|
40
41
|
},
|
|
41
42
|
"bugs": {
|
|
42
43
|
"url": "https://github.com/polotno-project/polotno-node/issues"
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const ffmpeg = require('fluent-ffmpeg');
|
|
3
|
+
const axios = require('axios');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
const downloadVideo = async (url, destination) => {
|
|
7
|
+
const response = await axios({
|
|
8
|
+
method: 'GET',
|
|
9
|
+
url: url,
|
|
10
|
+
responseType: 'stream',
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const writer = response.data.pipe(fs.createWriteStream(destination));
|
|
14
|
+
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
writer.on('finish', resolve);
|
|
17
|
+
writer.on('error', reject);
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// 2. Convert it into webm format
|
|
22
|
+
|
|
23
|
+
const convertToWebM = (input, output) => {
|
|
24
|
+
console.log(input, output);
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
ffmpeg(input)
|
|
27
|
+
.outputOptions(['-c:v libvpx', '-crf 30', '-b:v 1000k'])
|
|
28
|
+
.output(output)
|
|
29
|
+
.on('start', () => {})
|
|
30
|
+
.on('end', () => {
|
|
31
|
+
resolve();
|
|
32
|
+
})
|
|
33
|
+
.on('error', (err) => {
|
|
34
|
+
console.log('Error occurred: ' + err.message);
|
|
35
|
+
reject(err);
|
|
36
|
+
})
|
|
37
|
+
.run();
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// 3. Serialize file into dataurl string
|
|
42
|
+
const fileToDataUrl = (filename) => {
|
|
43
|
+
return new Promise((resolve, reject) => {
|
|
44
|
+
fs.readFile(filename, 'base64', (err, data) => {
|
|
45
|
+
if (err) reject(err);
|
|
46
|
+
resolve(`data:video/webm;base64,${data}`);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const videoToDataURL = async (url) => {
|
|
52
|
+
console.log('downloading', url);
|
|
53
|
+
const filename = Math.random().toString(36).substring(7);
|
|
54
|
+
const destination = path.join('./tmp', filename + '.mp4');
|
|
55
|
+
await downloadVideo(url, destination);
|
|
56
|
+
const webmFile = destination.replace('.mp4', '.webm');
|
|
57
|
+
console.log('converting to webm', url);
|
|
58
|
+
await convertToWebM(destination, webmFile);
|
|
59
|
+
const dataUrl = await fileToDataUrl(webmFile);
|
|
60
|
+
return dataUrl;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
function printProgress(progress) {
|
|
64
|
+
process.stdout.clearLine(0);
|
|
65
|
+
process.stdout.cursorTo(0);
|
|
66
|
+
process.stdout.write('Rendering frame: ' + progress + '%');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports.jsonToVideo = async function jsonToVideo(
|
|
70
|
+
createInstance,
|
|
71
|
+
json,
|
|
72
|
+
attrs
|
|
73
|
+
) {
|
|
74
|
+
try {
|
|
75
|
+
fs.rmSync('./tmp', { recursive: true });
|
|
76
|
+
} catch (e) {}
|
|
77
|
+
|
|
78
|
+
fs.mkdirSync('./tmp', { recursive: true });
|
|
79
|
+
|
|
80
|
+
for (const page of json.pages) {
|
|
81
|
+
for (const el of page.children) {
|
|
82
|
+
if (el.type === 'video') {
|
|
83
|
+
el.src = await videoToDataURL(el.src);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const duration = json.pages.reduce((acc, page) => {
|
|
89
|
+
return acc + page.duration;
|
|
90
|
+
}, 0);
|
|
91
|
+
const parallel = attrs.parallel || 5;
|
|
92
|
+
|
|
93
|
+
const fps = attrs.fps || 15;
|
|
94
|
+
const timePerFrame = 1000 / fps;
|
|
95
|
+
|
|
96
|
+
// loop through the images and add each to the animation
|
|
97
|
+
const framesNumber = Math.floor((duration / 1000) * fps);
|
|
98
|
+
const frames = Array.from(Array(framesNumber).keys());
|
|
99
|
+
|
|
100
|
+
// split frames into parallel chunks
|
|
101
|
+
const chunkSize = Math.ceil(framesNumber / parallel);
|
|
102
|
+
const chunks = [];
|
|
103
|
+
for (let i = 0; i < framesNumber; i += chunkSize) {
|
|
104
|
+
chunks.push(frames.slice(i, i + chunkSize));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const pages = json.pages.map((p, index) => {
|
|
108
|
+
const previousPage = json.pages[index - 1];
|
|
109
|
+
const startTime = previousPage
|
|
110
|
+
? previousPage.startTime + previousPage.duration
|
|
111
|
+
: 0;
|
|
112
|
+
return {
|
|
113
|
+
id: p.id,
|
|
114
|
+
startTime,
|
|
115
|
+
duration: p.duration,
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
let finishedFramesNumber = 0;
|
|
120
|
+
|
|
121
|
+
await Promise.all(
|
|
122
|
+
chunks.map(async (chunk, chunkIndex) => {
|
|
123
|
+
const instance = await createInstance();
|
|
124
|
+
const page = await instance.createPage();
|
|
125
|
+
await page.evaluate(async (json) => {
|
|
126
|
+
store.loadJSON(json);
|
|
127
|
+
await store.waitLoading();
|
|
128
|
+
await new Promise((resolve) => {
|
|
129
|
+
setTimeout(resolve, 100);
|
|
130
|
+
});
|
|
131
|
+
}, json);
|
|
132
|
+
|
|
133
|
+
let lastPageIndex = -1;
|
|
134
|
+
for (const frameIndex of chunk) {
|
|
135
|
+
let currentTime = frameIndex * timePerFrame;
|
|
136
|
+
if (frameIndex === 0) {
|
|
137
|
+
// offset the very first frame to enable animation start
|
|
138
|
+
currentTime = 1;
|
|
139
|
+
}
|
|
140
|
+
if (frameIndex === framesNumber - 1) {
|
|
141
|
+
// offset the very last frame to enable animation end
|
|
142
|
+
currentTime = duration - 1;
|
|
143
|
+
}
|
|
144
|
+
const storePageIndex = pages.findIndex((p) => {
|
|
145
|
+
return (
|
|
146
|
+
currentTime >= p.startTime && currentTime < p.startTime + p.duration
|
|
147
|
+
);
|
|
148
|
+
});
|
|
149
|
+
if (storePageIndex !== lastPageIndex) {
|
|
150
|
+
await page.evaluate(async (currentTime) => {
|
|
151
|
+
store.setCurrentTime(currentTime);
|
|
152
|
+
await store.waitLoading();
|
|
153
|
+
if (window.config.unstable_setTextOverflow) {
|
|
154
|
+
window.config.unstable_setTextOverflow('resize');
|
|
155
|
+
} else {
|
|
156
|
+
// console.error(
|
|
157
|
+
// 'Can not set text overflow mode. Define window.config.unstable_setTextOverflow function'
|
|
158
|
+
// );
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
const dataURL = await page.evaluate(
|
|
163
|
+
async (json, attrs, currentTime) => {
|
|
164
|
+
store.setCurrentTime(currentTime);
|
|
165
|
+
const currentPage = store.pages.find((p) => {
|
|
166
|
+
return (
|
|
167
|
+
store.currentTime >= p.startTime &&
|
|
168
|
+
store.currentTime < p.startTime + p.duration
|
|
169
|
+
);
|
|
170
|
+
});
|
|
171
|
+
const url = await store.toDataURL({
|
|
172
|
+
pixelRatio: 0.5,
|
|
173
|
+
...attrs,
|
|
174
|
+
pageId: currentPage?.id,
|
|
175
|
+
});
|
|
176
|
+
return url;
|
|
177
|
+
},
|
|
178
|
+
json,
|
|
179
|
+
attrs || {},
|
|
180
|
+
currentTime
|
|
181
|
+
);
|
|
182
|
+
fs.mkdirSync('./tmp', { recursive: true });
|
|
183
|
+
fs.writeFileSync(
|
|
184
|
+
`./tmp/${frameIndex}.png`,
|
|
185
|
+
dataURL.split(',')[1],
|
|
186
|
+
'base64'
|
|
187
|
+
);
|
|
188
|
+
finishedFramesNumber += 1;
|
|
189
|
+
const progress = ((finishedFramesNumber / framesNumber) * 100).toFixed(
|
|
190
|
+
1
|
|
191
|
+
);
|
|
192
|
+
printProgress(progress);
|
|
193
|
+
}
|
|
194
|
+
// TODO: why can't we just close the one created page?
|
|
195
|
+
// const browserPages = await instance.browser.pages();
|
|
196
|
+
// for (let i = 0; i < browserPages.length; i++) {
|
|
197
|
+
// await browserPages[i].close();
|
|
198
|
+
// }
|
|
199
|
+
await page.close();
|
|
200
|
+
if (!attrs.keepInstance) {
|
|
201
|
+
await instance.close();
|
|
202
|
+
}
|
|
203
|
+
})
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
// for (let i = 0; i < frames; i++) {
|
|
207
|
+
// let currentTime = i * timePerFrame;
|
|
208
|
+
// if (i === 0) {
|
|
209
|
+
// // offset the very first frame to enable animation start
|
|
210
|
+
// currentTime = 1;
|
|
211
|
+
// }
|
|
212
|
+
// if (i === frames - 1) {
|
|
213
|
+
// // offset the very last frame to enable animation end
|
|
214
|
+
// currentTime = duration - 1;
|
|
215
|
+
// }
|
|
216
|
+
|
|
217
|
+
// const dataURL = await page.evaluate(
|
|
218
|
+
// async (json, attrs, currentTime) => {
|
|
219
|
+
// store.setCurrentTime(currentTime);
|
|
220
|
+
// const currentPage = store.pages.find((p) => {
|
|
221
|
+
// return (
|
|
222
|
+
// store.currentTime >= p.startTime &&
|
|
223
|
+
// store.currentTime < p.startTime + p.duration
|
|
224
|
+
// );
|
|
225
|
+
// });
|
|
226
|
+
// const url = await store.toDataURL({
|
|
227
|
+
// pixelRatio: 0.5,
|
|
228
|
+
// ...attrs,
|
|
229
|
+
// pageId: currentPage?.id,
|
|
230
|
+
// });
|
|
231
|
+
// return url;
|
|
232
|
+
// },
|
|
233
|
+
// json,
|
|
234
|
+
// attrs || {},
|
|
235
|
+
// currentTime
|
|
236
|
+
// );
|
|
237
|
+
// const progress = ((i / (frames - 1)) * 100).toFixed(1);
|
|
238
|
+
// printProgress(progress);
|
|
239
|
+
// fs.mkdirSync('./tmp', { recursive: true });
|
|
240
|
+
// fs.writeFileSync(`./tmp/${i}.png`, dataURL.split(',')[1], 'base64');
|
|
241
|
+
// }
|
|
242
|
+
|
|
243
|
+
console.log('\nRendering video');
|
|
244
|
+
|
|
245
|
+
await new Promise((resolve, reject) => {
|
|
246
|
+
ffmpeg()
|
|
247
|
+
.input(`./tmp/%d.png`) // Make sure your images are named with numbers, e.g. 1.jpg, 2.jpg, etc.
|
|
248
|
+
.inputFPS(fps)
|
|
249
|
+
.videoCodec('libx264')
|
|
250
|
+
.outputOptions('-pix_fmt yuv420p')
|
|
251
|
+
.format('mp4')
|
|
252
|
+
.on('end', () => resolve())
|
|
253
|
+
.on('error', (err) => reject(err))
|
|
254
|
+
.save(attrs.out);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
fs.rmSync('./tmp', { recursive: true });
|
|
258
|
+
};
|
package/video.js
CHANGED
|
@@ -63,7 +63,7 @@ const videoToDataURL = async (url) => {
|
|
|
63
63
|
function printProgress(progress) {
|
|
64
64
|
process.stdout.clearLine(0);
|
|
65
65
|
process.stdout.cursorTo(0);
|
|
66
|
-
process.stdout.write('Rendering
|
|
66
|
+
process.stdout.write('Rendering frames: ' + progress + '%');
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
module.exports.jsonToVideo = async function jsonToGifFile(page, json, attrs) {
|
|
@@ -87,7 +87,11 @@ module.exports.jsonToVideo = async function jsonToGifFile(page, json, attrs) {
|
|
|
87
87
|
await store.waitLoading();
|
|
88
88
|
// TODO: we need to active all pages first
|
|
89
89
|
// disable text fit, as it is already should be processed
|
|
90
|
-
window.config.unstable_setTextOverflow
|
|
90
|
+
if (window.config.unstable_setTextOverflow) {
|
|
91
|
+
window.config.unstable_setTextOverflow('resize');
|
|
92
|
+
} else {
|
|
93
|
+
console.error('Can not set text overflow mode');
|
|
94
|
+
}
|
|
91
95
|
},
|
|
92
96
|
json,
|
|
93
97
|
attrs || {}
|