python 如何用opencv连续保存1分钟名为日期和时间的视频

h5qlskok  于 2023-03-07  发布在  Python
关注(0)|答案(1)|浏览(182)

我想每分钟重复地创建一个新的以当前日期和时间命名的录像文件保存到目录路径中。
在下面的代码中,它所能做的就是制作一个长度为1分钟的视频。

  1. import cv2
  2. import time
  3. from datetime import datetime
  4. import schedule
  5. import os
  6. import sys
  7. import numpy as np
  8. from os import path
  9. cap = cv2.VideoCapture(0)
  10. if (cap.isOpened() == False):
  11. print("Error reading video file")
  12. fps = 24
  13. res = '480p'
  14. def change_res(cap, width, height):
  15. cap.set(3, width)
  16. cap.set(4, height)
  17. STD_DIMENSIONS = {"480p": (640, 480),"720p": (1280, 720),}
  18. def get_dims(cap, res='720p'):
  19. width, height = STD_DIMENSIONS["480p"]
  20. if res in STD_DIMENSIONS:
  21. width,height = STD_DIMENSIONS[res]
  22. change_res(cap, width, height)
  23. return width, height
  24. PREFIX = 'Cam'
  25. EXTENSION = 'avi'
  26. file_name_format = "{:s}-{:%d-%m-%Y %H:%M-%S}.{:s}"
  27. date = datetime.now()
  28. today=time.strftime('%d-%m-%Y')
  29. file_name = file_name_format.format(PREFIX, date, EXTENSION)
  30. fourcc = cv2.VideoWriter_fourcc(*'XVID')
  31. out = cv2.VideoWriter ("/home/Desktop/CAM/"+str(today)+"/"
  32. +file_name, fourcc, 24.0, get_dims(cap, res))
  33. start_time = time.time()
  34. while (time.time() - start_time) < 60:
  35. ret, frame = cap.read()
  36. if ret == True:
  37. out.write(frame)
  38. cv2.imshow('CAM', frame)
  39. if cv2.waitKey(1) & 0xFF == ord('q'):
  40. break
  41. cap.release()
  42. out.release()
  43. cv2.destroyAllWindows()
7cjasjjr

7cjasjjr1#

只需将所有内容 Package 在while True:中,如下所示:

  1. import cv2
  2. import time
  3. from datetime import datetime
  4. import schedule
  5. import os
  6. import sys
  7. import numpy as np
  8. from os import path
  9. while True:
  10. cap = cv2.VideoCapture(0)
  11. if (cap.isOpened() == False):
  12. print("Error reading video file")
  13. fps = 24
  14. res = '480p'
  15. def change_res(cap, width, height):
  16. cap.set(3, width)
  17. cap.set(4, height)
  18. STD_DIMENSIONS = {"480p": (640, 480),"720p": (1280, 720),}
  19. def get_dims(cap, res='720p'):
  20. width, height = STD_DIMENSIONS["480p"]
  21. if res in STD_DIMENSIONS:
  22. width,height = STD_DIMENSIONS[res]
  23. change_res(cap, width, height)
  24. return width, height
  25. PREFIX = 'Cam'
  26. EXTENSION = 'avi'
  27. file_name_format = "{:s}-{:%d-%m-%Y %H:%M-%S}.{:s}"
  28. date = datetime.now()
  29. today=time.strftime('%d-%m-%Y')
  30. file_name = file_name_format.format(PREFIX, date, EXTENSION)
  31. fourcc = cv2.VideoWriter_fourcc(*'XVID')
  32. out = cv2.VideoWriter (file_name, fourcc, 24.0, get_dims(cap, res))
  33. start_time = time.time()
  34. while (time.time() - start_time) < 5:
  35. ret, frame = cap.read()
  36. if ret == True:
  37. out.write(frame)
  38. cv2.imshow('CAM', frame)
  39. if cv2.waitKey(1) & 0xFF == ord('q'):
  40. break
  41. cap.release()
  42. out.release()
  43. cv2.destroyAllWindows()

或者你可以在循环中添加一个计数器来代替while True:,例如当计数器达到某个值时跳出循环。

展开查看全部

相关问题