main.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from asyncio.windows_events import NULL
  2. import os
  3. import sys
  4. from selenium import webdriver
  5. import time
  6. import threading
  7. from PIL import ImageGrab
  8. from moviepy.editor import ImageSequenceClip
  9. fileid = 0 #csv文件编号 详见 fileinfo.js
  10. picpath = r"pic\\%d" % fileid
  11. videopath = r"video"
  12. screenshotArea = (635, 338, 1285, 828) #屏幕截图范围 (起点x坐标,起点y坐标, 终点x坐标, 终点y坐标)
  13. imageList = []
  14. driver = NULL
  15. totalTime = 0 # 心跳记录的总时长 秒
  16. endFlag = 0 # 0:未开始 1:已开始 2:已结束
  17. frameIndex = 0
  18. iswork = 0
  19. threadNum = 0
  20. intverTime = 0.03 # 截图间隔时间 秒
  21. i = 0
  22. def save_screenshot():
  23. global driver, fileid, picpath, screenshotArea, imageList, totalTime, endFlag, frameIndex, iswork, threadNum, intverTime, i
  24. try:
  25. if iswork == 0:
  26. return
  27. threadNum += 1
  28. t = threading.Timer(intverTime, save_screenshot) # 帧率: 25张/秒
  29. t.start()
  30. if i >= 25:
  31. i = 0
  32. if i == 0:
  33. # totalTime = int(driver.find_element_by_id("totalTime").get_attribute('value'))
  34. endFlag = int(driver.find_element_by_id("endFlag").get_attribute('value'))
  35. i += 1
  36. # print("[%s] %d" % (time.strftime('%H:%M:%S', time.localtime(time.time())), frameIndex))
  37. # print("[%s][%d] %d / %d" % (time.strftime('%H:%M:%S', time.localtime(time.time())), frameIndex, rowNum, rsCount))
  38. if endFlag == 1:
  39. # if intverTime * i <= totalTime:
  40. frameIndex += 1
  41. # file = r"%s\\%s_%s_%d.png" % (picpath, ('00000' + str(frameIndex))[-5:], time.strftime('%H%M%S', time.localtime(time.time())), i)
  42. file = r"%s\\%s.png" % (picpath, ('00000' + str(frameIndex))[-5:])
  43. imageList.append(file)
  44. im = ImageGrab.grab(screenshotArea)
  45. im.save(file)
  46. print("save_screenshot: " + file)
  47. elif endFlag == 2:
  48. iswork = 0
  49. threadNum -= 1
  50. if iswork == 0 and threadNum <= 0:
  51. print("save_screenshot finished !!")
  52. print("threadNum: %d" % threadNum)
  53. driver.close()
  54. driver.quit()
  55. print("============ makeVideo ============")
  56. saveVideo()
  57. except BaseException as msg:
  58. print("[err] %s" % msg)
  59. threadNum -= 1
  60. iswork = 0
  61. if iswork == 0 and threadNum <= 0:
  62. driver.close()
  63. driver.quit()
  64. def saveVideo():
  65. global fileid, videopath
  66. clip = ImageSequenceClip(imageList, fps = 25)
  67. clip.write_videofile("%s\\%s.mp4" % (videopath, fileid), codec="libx264", bitrate="200000")
  68. def main():
  69. global driver, picpath, iswork
  70. try:
  71. if not os.path.exists(picpath):
  72. os.makedirs(picpath)
  73. driver = webdriver.Chrome(r"chromedriver.exe")
  74. picture_url = "http://localdata/?f=%d" % fileid
  75. driver.maximize_window()
  76. driver.get(picture_url)
  77. # driver.refresh()
  78. time.sleep(0.5)
  79. iswork = 1
  80. save_screenshot()
  81. # driver.close()
  82. # driver.quit()
  83. except BaseException as msg:
  84. print("[err] %s" % msg)
  85. driver.close()
  86. driver.quit()
  87. if __name__ == '__main__':
  88. paramNum = len(sys.argv)
  89. if paramNum != 2:
  90. print("param err! please input fileid.")
  91. exit()
  92. # sys.argv[0] 表示脚本名
  93. fileid = int(sys.argv[1]) #csv文件编号 详见 fileinfo.js
  94. picpath = r"pic\\%d" % fileid
  95. main()