Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Last OpenCV update (3.3.1) makes my videos faster

I have the following code, which just makes videos from a camera.

The user writes for example "python record_video.py 20" where that 20 is the length in seconds. In 3.3.0 the following script works fine, in 3.3.1 it makes 33% faster videos.

def random_filename(size):
    ''' to create random names for the dataset pictures '''

    w = ''.join(random.choice(string.ascii_lowercase) for i in range(size))
    return w+'.avi'


def kill_video(event, time_to_wait):
    '''Waits for time_wait time.
    Then sets and event so that the timeout is marked and the video recording
    stops'''
    time.sleep(time_to_wait+1) # cam always takes around 1s to warm up
    event.set()


if __name__ == "__main__":

    if len(sys.argv) > 1:
        user_time = float(sys.argv[1])
    else:
        user_time = 0

    vc = cv2.VideoCapture(0)
    vc.set(cv2.CAP_PROP_FRAME_WIDTH, 800)
    vc.set(cv2.CAP_PROP_FRAME_HEIGHT, 600)
    cv2.namedWindow("video")

    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    random_name = random_filename(5)
    out = cv2.VideoWriter(random_name,fourcc, 30.0, (800,600))

    event = threading.Event()

    if user_time > 0:
        timer = threading.Thread(target=kill_video, args=(event,user_time))
        timer.start()

    while(1):
        start_time = time.clock() * 1000 # seconds to ms
        ret,frame = vc.read()
        out.write(frame)
        cv2.imshow("video",frame)

        end_time = time.clock() * 1000
        wait_time = 33333 - (end_time - start_time)*1000
        key = cv2.waitKey(int(wait_time/1000))

        if key == ord('q') or event.is_set():
            break

    vc.release()
    out.release()
    cv2.destroyAllWindows()

As said, with the current version 3.3.1 it makes 33% videos faster, but I just downgraded to 3.3.0 and it makes videos the right time length.