Cv2. Videowriter Only Generate Empty File

Cv2. Videowriter Only Generate Empty File

When I tried to make a video by opencv2, I always end up having an empty file. My python version is 2.7.13 and opencv version is 3.2.0. I am using Windows. I tried the code by @Creyesk but still get an empty file.

import cv2
import cv2.cv as cv
import numpy as np

writer = cv2.VideoWriter('test1.avi',cv.CV_FOURCC('P','I','M','1'),25,  (640,480))
for i in range(1000):
    x = np.random.randint(255,size=(480,640)).astype('uint8')
    x = np.repeat(x,3,axis=1)
    x = x.reshape(480, 640, 3)
    writer.write(x)

Thanks so much!

2 Answers

The issue seems to be in your fourCC argument. I tried the following arguments and it worked:

writer=cv2.VideoWriter("test1.avi", cv.CV_FOURCC(*'DIVX'), 25, (640,480))

2

As mentioned in the comment by @Gerard, there can be many reasons for this, since errors during construction of the writer or when adding frames, will not be raised as exceptions.

To check whether the constructor worked, you can call writer.isOpened() and check if it's True.

Then still errors can happen during writing frames, like when the frame does not have the same shape as passed to the constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.