Mitchell van Zuylen
Mitchell van Zuylen

Reputation: 4105

cv2 VideoWriter returns corrupted video

There are a number of related questions, however none of the solutions appear to work. The primary solution I can find on SO is related to input size, which I believe I have corrected for. Nevertheless, I have the following issue:

I have a number of images that I want to use as frames:

frame_0001.png
frame_0002.png
frame_.....etc

I load them all in:

frames_location = glob.glob(r'stimuli\ambush_1\frame_*.png')
frames = []
for frame in frames_location:
    frames.append(cv2.imread(frame))

Next, I use the following function to convert this list of frames into a video:

def frames_to_video(frames, name='test.mp4'):
    
    w,h,_ = frames[0].shape # extract size information

    fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') 
    writer = cv2.VideoWriter(name, fourcc, 20,  (w,h) )

    for frame in frames:
        writer.write(frame)
    writer.release()

video = frames_to_video(frames)

Running this code creates a test.mp4 video as expect, which is 1kb large. The images I load in are each around 300kb so that's already a first signal something is wrong. Trying to play the video returns a 0xc10100be error.

Where I am I going wrong? What's wrong with my code?

I'm running Python 3.9.6 with a OpenCv 4.5.3 install.

Upvotes: 1

Views: 1058

Answers (1)

Mitchell van Zuylen
Mitchell van Zuylen

Reputation: 4105

Turned out I was missing a dll file.

More specifically, I had installed opencv using pip. However, I had not chosen the contrib version.

As such, to fix it was simple

py -m pip uninstall opencv-python
py -m pip install opencv-contrib-python

Upvotes: 3

Related Questions