Camera capture code not recording any video

Hi,
I’m working on an experiment in v3.2.4 and trying to add a camera to record the participants face while they are looking at a fixation cross, and then when they are viewing a stimulus. I’ve added the camera as a code component in Builder. I’ve successfully got it to save a video file for each presentation, but only the first video file generated actually has any content, and the rest are only a few hundred bytes and give me a 0xc10100be can’t play error when I open them. I haven’t been able to figure out why the recording only works for the first instance - here’s the code I’m using for the camera:

(begin experiment)

capture = cv2.VideoCapture(0)

(begin routine)
```fourcc = cv2.VideoWriter_fourcc(*'mp4v')
videoWriter = cv2.VideoWriter('C:\\Users\\hc122\\Desktop\\InteroStudy2020\\video captures\\IAPS_%s_%s_%s_%s.mp4' %(expInfo['Participant'],stimIAPS,condition,trialsPract.thisTrialN), fourcc, 30.0,(640,480))

(each frame)
```ret, frame = capture.read()
if ret:
    videoWriter.write(frame)

(end routine)
```capture.release()
videoWriter.release()
cv2.destroyAllWindows()

At a guess the problem is something to do with how the component ends as the camera is still lit up indicating it's in use after the stimulus ends and the participant has to fill out some responses, and I don't know if that's what is causing it to not record again?

Any help would be appreciated, thanks in advance!

Hi Eliza, I am having a similar issue, did you manage to solve your problem? Thanks in advance! Marjan

Hi, I can’t remember exactly how I solved it - looking at a more up to date version of the project I’d moved the code in end routine above to end experiment which I think is what made it work (though I don’t have access to the final version so can’t check for sure). I also don’t think you need the cv2.destroyAllWindows() line at all - I think that would close a webcam live feed display if one existed but this code doesn’t generate one so it should be fine without it. Other than moving those end routine lines to the end of the experiment the rest of my code is the same as it is here and it worked to capture individual videos at all the points I needed. One thing I will note is that I found it created a delay of several minutes on launching the experiment - I never found a fix for that but it didn’t affect the experiment once it started. Hopefully that helps - sorry I couldn’t be more sure!

Thank you so much @eeli.zaa for your reply. I never managed to get the experiment to start because it kept freezing. I tried waiting and nothing happened. I will try moving the code to the end experiment and see if it makes a difference for me as well. Thanks!