| 12345678910111213141516171819202122232425262728293031 |
- #!/usr/bin/env node
- /**
- * ADB 截图:根据 IP 截屏并保存到指定路径
- * 用法: node adb-screencap.js <ip> <outputPath>
- */
- const { spawnSync } = require('child_process')
- const path = require('path')
- const fs = require('fs')
- const configPath = process.env.STATIC_ROOT
- ? path.join(path.dirname(process.env.STATIC_ROOT), 'config.js')
- : path.join(__dirname, '..', '..', 'config.js')
- const projectRoot = path.dirname(path.resolve(configPath))
- const config = fs.existsSync(configPath) ? require(configPath) : {}
- const adb = config.adbPath?.path
- ? (path.isAbsolute(config.adbPath.path) ? config.adbPath.path : path.resolve(projectRoot, config.adbPath.path))
- : path.join(projectRoot, 'lib', 'scrcpy-adb', process.platform === 'win32' ? 'adb.exe' : 'adb')
- /** 截图并保存到 outputPath */
- function captureScreenshot(ip, outputPath) {
- const r = spawnSync(adb, ['-s', ip, 'exec-out', 'screencap', '-p'], { encoding: null, timeout: 10000, maxBuffer: 16 * 1024 * 1024 })
- fs.writeFileSync(outputPath, r.stdout)
- return { success: true }
- }
- if (require.main === module) {
- captureScreenshot(process.argv[2], process.argv[3])
- process.stdout.write(process.argv[3])
- }
- module.exports = { captureScreenshot }
|