adb-screencap.js 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/env node
  2. /**
  3. * ADB 截图:根据 IP 截屏并保存到指定路径
  4. * 用法: node adb-screencap.js <ip> <outputPath>
  5. */
  6. const { spawnSync } = require('child_process')
  7. const path = require('path')
  8. const fs = require('fs')
  9. const configPath = process.env.STATIC_ROOT
  10. ? path.join(path.dirname(process.env.STATIC_ROOT), 'config.js')
  11. : path.join(__dirname, '..', '..', 'config.js')
  12. const projectRoot = path.dirname(path.resolve(configPath))
  13. const config = fs.existsSync(configPath) ? require(configPath) : {}
  14. const adb = config.adbPath?.path
  15. ? (path.isAbsolute(config.adbPath.path) ? config.adbPath.path : path.resolve(projectRoot, config.adbPath.path))
  16. : path.join(projectRoot, 'lib', 'scrcpy-adb', process.platform === 'win32' ? 'adb.exe' : 'adb')
  17. /** 截图并保存到 outputPath */
  18. function captureScreenshot(ip, outputPath) {
  19. const r = spawnSync(adb, ['-s', ip, 'exec-out', 'screencap', '-p'], { encoding: null, timeout: 10000, maxBuffer: 16 * 1024 * 1024 })
  20. fs.writeFileSync(outputPath, r.stdout)
  21. return { success: true }
  22. }
  23. if (require.main === module) {
  24. captureScreenshot(process.argv[2], process.argv[3])
  25. process.stdout.write(process.argv[3])
  26. }
  27. module.exports = { captureScreenshot }