# Sync while-loop iterations to frame rate

**URL:** <https://discourse.psychopy.org/t/sync-while-loop-iterations-to-frame-rate/11113>\
**Category:** Coding\
**Tags:** timing, frame-rate\
**Created:** [February 20, 2020, 10:42pm UTC](https://discourse.psychopy.org/t/sync-while-loop-iterations-to-frame-rate/11113 "2020-02-20T22:42:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlevitas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dlevitas/32/1422_2.png) [@dlevitas](https://discourse.psychopy.org/u/dlevitas)\
**Post date:** [February 20, 2020, 10:42pm UTC](https://discourse.psychopy.org/t/sync-while-loop-iterations-to-frame-rate/11113/1 "2020-02-20T22:42:15Z")

</div>

This is a cross post from [here](https://stackoverflow.com/questions/60303825/cv2-cant-create-video-of-specific-duration), 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:

```auto
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](https://www.psychopy.org/recipes/webcam.html) 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

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [February 20, 2020, 11:55pm UTC](https://discourse.psychopy.org/t/sync-while-loop-iterations-to-frame-rate/11113/2 "2020-02-20T23:55:11Z")

</div>

As indicated in the StackOverflow post, the issue is likely that the iterations of the `while` loop are not governed in any way. You should insert a counter in that loop to monitor how many frames you are grabbing within the 30 s loop, which is also likely to vary somewhat from one run to another. One solution would be to create a PsychoPy window and issue a `win.flip()` on every iteration. This would slow the loop to a hardware cycle (your screen refresh rate). Your counter should then show a reliable rate of, say, 60 Hz, assuming that the read and write functions can be completed within a 16.7 ms period. You could then tell the video writer to save the video at that frame rate, so the playback speed would then match the recorded speed.

Alternatively, you could use timers, but that gets tricky, as you’ll need to account for temporal slippage as the code runs. The naturally discretised time line provided by syncing to the screen refresh rate solves a lot of this for you.

---

<div class="post-metadata">

**Author:** ![dlevitas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dlevitas/32/1422_2.png) [@dlevitas](https://discourse.psychopy.org/u/dlevitas)\
**Post date:** [February 21, 2020, 3:49pm UTC](https://discourse.psychopy.org/t/sync-while-loop-iterations-to-frame-rate/11113/3 "2020-02-21T15:49:48Z")

</div>

Thanks @Michael

I should’ve indicated before that participants are viewing and describing images on a screen for 30 seconds. This means that in addition to video recording I’m also recording audio. I was able to adjust my while loop so that I can limit the video recording to 30 seconds; this entailed ending the loop once 600 frames are reached (with 20 fps). My lingering issue though is that the microphone recording is 21-sec instead of 30 (before it was 30-sec, but the video was 43-sec).

Here’s an updated (and more detailed) version of my code:

```auto
from __future__ import division, print_function
from psychopy import prefs
prefs.hardware['audioLib'] = ['pyo']
from psychopy import locale_setup, visual, core, event, data, gui, microphone, sound
import os, cv2

#Window:
win = visual.Window(size = (1200,1200), fullscr = True, pos = (0,0), units = "height", color = [-1,-1,-1])

#Turn off Mouse:
event.Mouse(visible = False)

#Set base variables:
width, height = [1200,1200]
text = visual.TextStim(win=win, text='', height=height/22, pos=(0,0), color="White")
microphone.switchOn()
mic = microphone.AdvAudioCapture()

for i in range(counter, len(stim_df)):
        image = visual.ImageStim(win=win, pos=(0,0), size=(0.55,0.55), image='Image{}'.format(i))

        #Begin audio recording:
        mic.reset()
        mic.record(sec=30, filename='audio_file{}'.format(i))
        
        #Begin Video Recording:
        cap = cv2.VideoCapture(0 + cv2.CAP_DSHOW)
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter('video_file{}.avi'.format(i), fourcc, 20.0, (640,480))

        #Present Image:
        theseKeys = event.clearEvents(eventType = "keyboard")
        image.setAutoDraw(True)

        frames = 0
        while True:
            ret, frame = cap.read()
            out.write(frame)
            win.flip()
            frames += 1

            if frames > 600:
                break

        #Stop audio & video recording after each trial:
        image.setAutoDraw(False)
        cap.release()
        out.release()
        cv2.destroyAllWindows()
        mic.stop()        

```

The code above keeps my video at 30 seconds and appears smooth; however, the audio is only 21-seconds and can’t be synced to the video. I’m unsure which microphone parameter(s) to change to be at the same rate as the video capture.

---

<div class="post-metadata">

**Author:** ![dlevitas](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dlevitas/32/1422_2.png) [@dlevitas](https://discourse.psychopy.org/u/dlevitas)\
**Post date:** [February 28, 2020, 2:28pm UTC](https://discourse.psychopy.org/t/sync-while-loop-iterations-to-frame-rate/11113/4 "2020-02-28T14:28:00Z")

</div>

Just wanted to ping this to see if anyone knows of a solution for syncing the video webcam and microphone capture

---

<div class="post-metadata">

**Author:** ![ivan866](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/ivan866/32/42137_2.png) [@ivan866](https://discourse.psychopy.org/u/ivan866)\
**Post date:** [April 26, 2020, 6:52pm UTC](https://discourse.psychopy.org/t/sync-while-loop-iterations-to-frame-rate/11113/5 "2020-04-26T18:52:58Z")

</div>

@dlevitas, one workaround is to capture the screen with some reliable software (I used MediaLooks for VirtualDub). The trick is that this software records both screen and desktop sound (as in streaming software nowadays), and they are in sync. That means you can fire a sync tone for the Microphone class in `psychopy`, which will be recorded both in your screen recording AND in sound channel you’re saving.  
The picture shows a slightly more broad problem: syncing two multi-channel data streams to each other; still involving yours, too. Note how microphone capture is synced to video source. Also note that error will depend mostly on your video framerate.

 ![DataCollectionScheme](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/c/e/ce8d54d5c2c325ed71a01de81d71dfc74e186531.png)
