0

I'm trying to record videos usin OpenCV in Python on Ubuntu 16.4. I'm using a Logitec 920 camera. My issue is that when I change the room illumination (from white to red light) or change the frame rate, the duration of the recorded video is altered becoming faster or slower dependingo on the situation. This is the code I'm using:

import numpy as np
import cv2
import time     

cam = cv2.VideoCapture(0) # select the camera

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID') # (*'XVID')
out = cv2.VideoWriter('teste6.avi',fourcc, 30.0, (640,480))

# Start time in seconds
t0 = time.time()

while (cam.isOpened()):
    ret, frame=cam.read() # read frames
    if ret == True:

    out.write(frame) 
    cv2.imshow('video',frame) # plot frames       

    t1 = time.time() # Current time
    dur = t1-t0; # diff time

    if dur > 60:
        out.release() # Stop video recording  
        print('end')

    if cv2.waitKey(1) & 0xFF== ord('q'): # close window pressing 'q' key
        break        

cam.release()
cv2.destroyAllWindows()

Any help?

1 Answers1

0

At a first glance, you should not incorporate writing/output in your record time. You are assuming that writing each frame and imshow are always going to return in the same amount of time. If you want a consistent read duration time, you should at a minimum cache your read in frames and then write them out after you are finished reading. If you want to display at the same time, you would have to do a more complicated threading adventure.

wrestang
  • 553
  • 7
  • 17
  • I tried to run only the write part and the problem persisted. I think this is relationed to the camera shutter. Is there any command in opencv that I can use to control it? – Rafael Bessa Apr 05 '18 at 21:54