This is a cross post from here, for posterity sake, and because I think/hope that the solution lies in Psychopy. I’m using a webcam to record a 30-sec video; however, the resulting video ends up being ~43-44 seconds. Here is my code:
from psychopy import locale_setup, visual, core, event, data, gui, microphone
import cv2
#Set up Timer:
trialTimer = core.Clock()
#Begin Video Recording:
cap = cv2.VideoCapture(0 + cv2.CAP_DSHOW)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('{}.avi'.format(video_path),fourcc, 20.0, (640,480))
trialTimer.reset()
while trialTimer.getTime() < 30:
ret, frame = cap.read()
out.write(frame)
#Stop video recording:
cap.release()
out.release()
cv2.destroyAllWindows()
The third argument in the cv2.VideoWriter
function specifies the frame rate, which is default at 20. This provides a smooth, fluid video, but one that is longer than 30 seconds. Adjusting this value to 29.433 makes the video 30 seconds, but it appears sped-up. The issue seems to be that I can’t constrain the while loop iterations to the frame rate or something else. Thus the frames are added to the video at a rate that I can’t control.
This post was helpful for getting the webcam recording to work; however, I made some changes since I’m not presenting the video, just saving it. I’ve tried a few things with no luck, and was wondering if someone has a solution to this issue.
Thanks for the help.
Dan