Ask Your Question

Last OpenCV update (3.3.1) makes my videos faster

asked 2017-11-01 06:48:10 -0500

the_phet gravatar image

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

The user writes for example "python 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
    time.sleep(time_to_wait+1) # cam always takes around 1s to warm up

if __name__ == "__main__":

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

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

    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))

        start_time = time.clock() * 1000 # seconds to ms
        ret,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():


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.

edit retag flag offensive close merge delete



Are you talking about the time the videos take to display? Or the actual length they play when you open the output file?

This code appears to have a constant framerate for the output file, which means it should be the same length no matter what version or what number you give it.

And of course, OpenCV is not meant for displaying videos. So you can't trust imshow and waitKey to show at a constant rate. It's simply not built for it.

Tetragramm gravatar imageTetragramm ( 2017-11-01 09:44:18 -0500 )edit

I have seen code from stackoverflow

supra56 gravatar imagesupra56 ( 2017-11-01 22:23:57 -0500 )edit

@Tetragramm If I produce a video with that script in 3.3.0, and then I play it with VLC, it will be 20 seconds (as expected). If I produce a video with the same script, but in 3.3.1, then play it in VLC the video is 13 seconds, and plays 33% faster.

the_phet gravatar imagethe_phet ( 2017-11-02 04:53:11 -0500 )edit

Well that's weird. Can you collect some debugging information? In the script, print out how many frames are written. In VLC, can you look in the codec information and get the Frame rate of the video? For both versions of OpenCV please.


Tetragramm gravatar imageTetragramm ( 2017-11-02 08:51:51 -0500 )edit

@Tetragramm Always aiming for a video of 20 seconds. Using 3.3.0 it writes 614 frames. In VLC it produces a video of 20 seconds and 30FPS. Using 3.3.1 it writes 413 frames. In VLC it produces a video of 13s and 30FPS.

This is for exactly the same code, but using 3.3.0 or 3.3.1. In the 3.3.1 video the action of what you see happens a 33% faster.

the_phet gravatar imagethe_phet ( 2017-11-03 11:37:35 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-11-03 12:14:11 -0500

Tetragramm gravatar image

Ok, I think I understand the problem. The processing of creating and writing the video is taking longer. Why? Not sure.

What you can do to help is change the value in waitKey. If you call waitKey(1), you will still see the frame on the screen, but it won't block the CPU from processing the next frame. As it is, the program is waiting at waitKey and not reading the next frame, resulting in the skips you see.

edit flag offensive delete link more


@Tetragramm its been some time, but I never got to fix this. setting waitkey to just 1 still makes the videos a 33% faster. the only thing that works is rolling back to 3.3.0

the_phet gravatar imagethe_phet ( 2018-07-23 06:14:33 -0500 )edit

I'm afraid I don't know then. Do please file a bug report HERE.

Tetragramm gravatar imageTetragramm ( 2018-08-20 21:39:46 -0500 )edit

Question Tools

1 follower


Asked: 2017-11-01 06:48:10 -0500

Seen: 1,486 times

Last updated: Nov 03 '17