我正在尝试使用OpenCV的cv2.VideoWriter来记录一段视频,但是输出结果不正确。例如,10秒的视频只有2秒,而且会加速播放。以下是我的代码。欢迎任何建议和想法。另外,输出视频没有声音。谢谢!!!
主机:树莓派
语言:Python
import numpy as np
import cv2
import time
# Define the duration (in seconds) of the video capture here
capture_duration = 10
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output3.avi',fourcc, 20.0, (640,480))
start_time = time.time()
while( int(time.time() - start_time) < capture_duration ):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()