我试图在Ubuntu 16.4上用Python在OpenCV上录制视频。我使用的是Logitec 920相机。我的问题是,当我改变房间照明(从白光到红光)或改变帧速率时,录制视频的持续时间会根据情况变得更快或更慢。这是我使用的代码:
import numpy as np
import cv2
import time
cam = cv2.VideoCapture(0) # select the camera
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID') # (*'XVID')
out = cv2.VideoWriter('teste6.avi',fourcc, 30.0, (640,480))
# Start time in seconds
t0 = time.time()
while (cam.isOpened()):
ret, frame=cam.read() # read frames
if ret == True:
out.write(frame)
cv2.imshow('video',frame) # plot frames
t1 = time.time() # Current time
dur = t1-t0; # diff time
if dur > 60:
out.release() # Stop video recording
print('end')
if cv2.waitKey(1) & 0xFF== ord('q'): # close window pressing 'q' key
break
cam.release()
cv2.destroyAllWindows()
任何帮助?
答案 0 :(得分:0)
乍一看,你不应该在录制时间内加入写作/输出。你假设写每个帧和imshow总是会在相同的时间内返回。如果您希望获得一致的读取持续时间,则应至少缓存读取的帧,然后在读完后将其写出。如果你想同时显示,你将不得不进行更复杂的线程冒险。