grab_frames.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/env python3
  2. # grab_frames.py <qmp socket> <out.txt> <seconds> <x0 y0 w h> - screendumps of the VM as fast as QEMU makes them,
  3. # each decoded to the frame number of the videotex kit's test video (7 blocks of the 640x360 source, decode.py
  4. # layout) inside the screen rectangle the 1920x1080 scene covers. Lines are "<seconds> <frame>", -1 = no number
  5. import os, sys, time
  6. import numpy as np
  7. sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
  8. from qmp import QMP
  9. def read_ppm(path):
  10. with open(path, 'rb') as f:
  11. data = f.read()
  12. parts = data.split(maxsplit=4)
  13. w, h = int(parts[1]), int(parts[2])
  14. return np.frombuffer(parts[4][:w * h * 3], np.uint8).reshape(h, w, 3).mean(2)
  15. sock, out, secs = sys.argv[1], sys.argv[2], float(sys.argv[3])
  16. x0, y0, w, h = map(float, sys.argv[4:8])
  17. pts = [(int(x0 + (20 + b * 88 + 36) * w / 640), int(y0 + 80 * h / 360)) for b in range(7)]
  18. q = QMP(sock)
  19. tmp = '/dev/shm/winvm_grab_%d.ppm' % os.getpid()
  20. res = []
  21. t0 = time.time()
  22. while time.time() - t0 < secs:
  23. q.cmd('screendump', filename=tmp)
  24. t = time.time() - t0
  25. a = read_ppm(tmp)
  26. bits = 0
  27. for px, py in pts:
  28. v = a[py - 3:py + 4, px - 3:px + 4].mean()
  29. bits = -1 if (bits < 0 or 60 < v < 190) else bits * 2 + (v >= 190)
  30. res.append((t, bits))
  31. os.unlink(tmp)
  32. open(out, 'w').write(''.join('%.4f %d\n' % r for r in res))