polotno-node 2.10.0 → 2.10.2

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/video.js DELETED
@@ -1,161 +0,0 @@
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 frames: ' + progress + '%');
67
- }
68
-
69
- module.exports.jsonToVideo = async function jsonToGifFile(page, json, attrs) {
70
- try {
71
- fs.rmSync('./tmp', { recursive: true });
72
- } catch (e) {}
73
-
74
- fs.mkdirSync('./tmp', { recursive: true });
75
-
76
- for (const page of json.pages) {
77
- for (const el of page.children) {
78
- if (el.type === 'video') {
79
- el.src = await videoToDataURL(el.src);
80
- }
81
- }
82
- }
83
- // first load json
84
- await page.evaluate(
85
- async (json, attrs) => {
86
- store.loadJSON(json);
87
- await store.waitLoading();
88
- // TODO: we need to active all pages first
89
- // disable text fit, as it is already should be processed
90
- if (window.config.unstable_setTextOverflow) {
91
- window.config.unstable_setTextOverflow('resize');
92
- } else {
93
- console.error('Can not set text overflow mode');
94
- }
95
- },
96
- json,
97
- attrs || {}
98
- );
99
-
100
- const duration = await page.evaluate(async () => {
101
- return store.duration;
102
- });
103
-
104
- const fps = 15;
105
- const timePerFrame = 1000 / fps;
106
-
107
- // loop through the images and add each to the animation
108
- const frames = Math.floor((duration / 1000) * fps);
109
- for (let i = 0; i < frames; i++) {
110
- let currentTime = i * timePerFrame;
111
- if (i === 0) {
112
- // offset the very first frame to enable animation start
113
- currentTime = 1;
114
- }
115
- if (i === frames - 1) {
116
- // offset the very last frame to enable animation end
117
- currentTime = duration - 1;
118
- }
119
-
120
- const dataURL = await page.evaluate(
121
- async (json, attrs, currentTime) => {
122
- store.setCurrentTime(currentTime);
123
- const currentPage = store.pages.find((p) => {
124
- return (
125
- store.currentTime >= p.startTime &&
126
- store.currentTime < p.startTime + p.duration
127
- );
128
- });
129
- const url = await store.toDataURL({
130
- pixelRatio: 0.5,
131
- ...attrs,
132
- pageId: currentPage?.id,
133
- });
134
- return url;
135
- },
136
- json,
137
- attrs || {},
138
- currentTime
139
- );
140
- const progress = ((i / (frames - 1)) * 100).toFixed(1);
141
- printProgress(progress);
142
- fs.mkdirSync('./tmp', { recursive: true });
143
- fs.writeFileSync(`./tmp/${i}.png`, dataURL.split(',')[1], 'base64');
144
- }
145
-
146
- console.log('\nRendering video');
147
-
148
- await new Promise((resolve, reject) => {
149
- ffmpeg()
150
- .input(`./tmp/%d.png`) // Make sure your images are named with numbers, e.g. 1.jpg, 2.jpg, etc.
151
- .inputFPS(fps)
152
- .videoCodec('libx264')
153
- .outputOptions('-pix_fmt yuv420p')
154
- .format('mp4')
155
- .on('end', () => resolve())
156
- .on('error', (err) => reject(err))
157
- .save(attrs.out);
158
- });
159
-
160
- fs.rmSync('./tmp', { recursive: true });
161
- };