How to create a camera recording for the duration of a variable routine/trial?

Hi!

I am running a study which is a combination of two tasks (each in its separate routine) and survey questions (also in separate routines). The participant is tasked to evaluate a physical product and therefore the task duration is variable. I.e., the routine ends when the participant feels they have competed the task and click an ‘ok’-buttton to proceed to the next routine. I want to have a camera recording of the participant as they complete the task. Obviously, the duration if the camera component must be set to the duration of the routine, which varies between particpants. I have tried a few things with code (explained below), but none have worked. Does anyone have any insight on how this might be solved?

OS: Windows 10
PsychoPy version: v2023.2.3
Standard Standalone? y
What are you trying to achieve?: Create a camera recording for the duration of a routine that varies in duration based on individual participants.

What did you try to make it work?: I tried adding a code component to calculate the duration of the routine. In the code component I wrote the following:
Before experiment:

import time as time
def total_seconds():
     return int(time.time())

Begin routine:
start_time = total_seconds()
Each frame:
elapsed_time = total_seconds() - start_time
End routine:
trial_time = elapsed_time

Then I tried setting the duration of the camera component to $trial_time, and to $elapsed_time, but neither worked.

What specifically went wrong when you tried that?:
In both cases PsychoPy stalled and eventually crashed. I got the following error messages:

UnboundLocalError: local variable 'trial_time' referenced before assignment
################ Experiment ended with exit code 1 [pid:48648] #################
UnboundLocalError: local variable 'elapsed_time' referenced before assignment
############ Experiment ended with exit code 3489660927 [pid:7312] #############

I tried another approach. I set the duration of the camera component to 9999 seconds (much more than needed). When the ok-button is clicked it forces the end of the rutine and therfore also the camera recording. This seems to work well enough when I use an external webcamera to record video. When I use the interal laptop webcamera the recorded video is 5-12 seconds longer than the routine, but the video freezes approx. at the time when the routine ended and leaves only still image of the video feed or a black screen. The current approach does not seem like the best one, so I would still highly appreciate any suggestions!

Morover, this approach only works when I try to record one routine. As mentioned I have two task routines which I’d like to video record. When I try to record video for both routines PsychoPy stalls at the “Attempting to measure frame rate…”-screen in the beginning, and it produced the following error message:

Alert 4330: Recording device '6' not found, using default device instead.

PTB-ERROR: Failed to open audio device 6. PortAudio reports this error: Invalid device 

2024-03-06 11:46:24.674 (   0.001s) [         21415D8]             common.cpp:64    INFO| git:a0fc2fde92a10f4cb5fccbc5552228b865f17379/branch:refs/tags/v1.15.0/build:Release/compiler:MSVC-19.0.24245.0/link:SHARED
Traceback (most recent call last):
  File "filepath\survey_v01_lastrun.py", line 2244, in <module>
    run(
  File "filepath\survey_v01_lastrun.py", line 554, in run
    defaultMicrophone = sound.microphone.Microphone(
  File "C:\Program Files\PsychoPy\lib\site-packages\psychopy\sound\microphone.py", line 504, in __init__
    self._stream = audio.Stream(

I don’t think the first alert is a problem (Alert 4330: Recording device '6' not found, using default device instead.) because this appears also when I am recording one routine, but it still works and I am able to record the video.

So it seems to be a problem with the audio. The audio setting are the same in both routines and globally in PsychoPy I have not changed any settings. Does anyone have any suggestions for how to solve this?

Thanks!