main.py 3.4 KB

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