如何在录制时让视频暂停/继续?

时间:2016-12-12 16:33:50

标签: python opencv

如何在OpenCV Python录制时让视频暂停/继续?

import numpy as np
import cv2
cap = cv2.VideoCapture(0)
 fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

while (cap):
   # Capture frame-by-frame
   ret, frame = cap.read()

   out.write(frame)
  # Display the resulting frame
   cv2.imshow('video recording', frame)

   if cv2.waitKey(1) & 0xFF == ord('q'):
       break
   # When everything done, release the capture
     cap.release()
    out.release()
    cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:3)

您将从VideoCapture实时Feed中获得连续帧。您可以设置一个标志来决定是否应该写入帧:

import numpy as np
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
isRecording=true
while (cap):
   # Capture frame-by-frame
   ret, frame = cap.read()
   if(isRecording):#read the boolean to decide whether to write frame or not
        out.write(frame)
  # Display the resulting frame
   cv2.imshow('video recording', frame)

   if cv2.waitKey(1) & 0xFF == ord('q'):
       break

   if cv2.waitKey(1) & 0xFF == ord('p'):#Pause
       isRecording=false
   if cv2.waitKey(1) & 0xFF == ord('c'):#Continue
       isRecording=true

   # When everything done, release the capture
    cap.release()
    out.release()
    cv2.destroyAllWindows()
相关问题