I'm trying to use opencv's cv2.VideoWriter to record a video for certain time. The problem is the output is incorrect. For example, the video for 10 seconds only got 2 seconds, and it plays faster like speed up. Here's my code. Any suggestions or ideas are welcome. Also, another problem is that the output video is silence. Thanks !!!
Host: Raspberry Pi
Language: 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()