forked from HCLonely/live2dNodeApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreenShot.js
67 lines (61 loc) · 1.9 KB
/
screenShot.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
/*
* @Author: HCLonely
* @Date: 2021-01-26 19:34:37
* @LastEditTime: 2021-09-01 15:20:51
* @LastEditors: HCLonely
* @Description: 生成模型预览图
* @FilePath: \live2dNodeApi\screenShot.js
*/
(async () => {
let chrome = {}
// let isAws = false
let puppeteer
const lanuchOptions = {
defaultViewport: {
width: 280,
height: 250
},
args: ['--no-sandbox', `--window-size=${280},${250}`]
}
if (process.env.AWS_LAMBDA_FUNCTION_VERSION) {
// running on the Vercel platform.
// isAws = true
chrome = require('chrome-aws-lambda')
puppeteer = require('puppeteer-core')
lanuchOptions.executablePath = await chrome.executablePath
} else {
// running locally.
puppeteer = require('puppeteer')
}
const fs = require('fs-extra')
const { exec, spawn } = require('child_process')
console.log('正在启动api服务器...')
const ls = exec('node index.js')
ls.stdout.on('data', function (data) {
if (data.toString().includes('Listening on port')) {
console.log('启动api服务器成功,即将开始截图')
screenShot()
}
})
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString())
})
async function screenShot () {
const { port } = fs.readJsonSync('config.json')
const browser = await puppeteer.launch(lanuchOptions)
const page = await browser.newPage()
const num = fs.readJsonSync('modelList.json').models.length
for (let i = 1; i <= num; i++) {
console.log('正在截图', i + '/' + num)
await page.goto('http://127.0.0.1:' + port + '/preview.html?id=' + i, { waitUntil: 'networkidle0' })
await page.screenshot({ path: 'static/screenshot/' + i + '.png' })
}
await browser.close()
if (/^win/.test(process.platform)) {
spawn('taskkill', ['/PID', ls.pid, '/T', '/F'])
} else {
ls.kill('SIGTERM')
}
console.log('截图任务完成')
}
})()