-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (117 loc) · 3.51 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
const puppeteer = require('puppeteer');
const ffmpegStatic = require('ffmpeg-static');
const ffmpeg = require('fluent-ffmpeg');
const fs = require('fs');
const path = require('path');
const fps = 24;
const width = 1920;
const height = 1080;
const omitBackground = true;
const numberOfConcurrency = require('os').cpus().length;
// const numberOfConcurrency = 4;
const data_urL_prefix = 'data:image/png;base64,';
const url = "http://localhost:5173/video";
const dirname = "temp_images"
const outputFileName = "test.mov";
process.setMaxListeners(numberOfConcurrency)
/**
* @param {number} startIndex starting index, used as start value for incrementing
*/
async function DOWNLOAD_AND_SAVE_IMAGES (startIndex) {
const browser = await puppeteer.launch({
headless : 'new',
args: ['--no-sandbox', '--disable-dev-shm-usage'],
});
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'load' })
// this waits until the "getInfo" function exists which means "seekFrame" will also exist
const info = await page.evaluate(`(async () => {
let deadline = Date.now() + 10000
while (Date.now() < deadline) {
if (typeof getInfo === 'function') {
break
}
await new Promise(r => setTimeout(r, 1000))
}
const info = await getInfo()
if (!info.width || !info.height) {
Object.assign(info, {
width: document.querySelector('#scene').offsetWidth,
height: document.querySelector('#scene').offsetHeight,
})
}
return info
})()`);
await page.setViewport({
width: width,
height: height,
deviceScaleFactor: 1,
});
const totalFrames = info.numberOfFrames
const imagesPerWorker = Math.floor(totalFrames / numberOfConcurrency);
const remainder = Math.floor(totalFrames % numberOfConcurrency);
// if the last one, then it should be responsible for the remainder of images
const amount = startIndex === numberOfConcurrency - 1
? imagesPerWorker + remainder
: imagesPerWorker;
const start = startIndex * imagesPerWorker
// outputting images to folder
for (let i = 0; i < amount; i++) {
const frame = start + i
const result = await page.evaluate(`seekToFrame(${frame})`);
const buffer = await page.screenshot({
clip: { x: 0, y: 0, width: width, height: height },
omitBackground: omitBackground,
});
const name = frame + ".png"
fs.writeFileSync(path.join(dirname, name), buffer);
}
await browser.close();
}
function CREATE_MOVIE () {
ffmpeg.setFfmpegPath(ffmpegStatic);
ffmpeg()
.input(dirname + '/%d.png')
.inputOptions(
// "-f", "image2pipe",
`-r`, fps,
)
.videoCodec("qtrle")
.saveToFile(outputFileName)
// Log the percentage of work completed
.on('progress', (progress) => {
if (progress.percent) {
console.log(`Processing: ${Math.floor(progress.percent)}% done`);
}
})
// The callback that is run when FFmpeg is finished
.on('end', () => {
console.log('FFmpeg has finished.');
})
// The callback that is run when FFmpeg encountered an error
.on('error', (error) => {
console.error(error);
});
}
function SETUP () {
if (!fs.existsSync(dirname)){
fs.mkdirSync(dirname);
}
else {
fs.rmSync(dirname, { recursive: true, force: true })
fs.mkdirSync(dirname);
}
}
async function CONCURRENTLY_DOWNLOAD () {
const promises = []
for (let i = 0; i < numberOfConcurrency; i++) {
promises.push(DOWNLOAD_AND_SAVE_IMAGES(i))
}
await Promise.all(promises);
}
async function RUN () {
SETUP()
await CONCURRENTLY_DOWNLOAD()
CREATE_MOVIE()
}
RUN()