2

First off, I know this question has been asked before but nothing I've found has worked.

I just want to make a video out of a series of matrices, and I thought i had written it all correctly, but it isn't working. Here is the code snippet I am using to try and debug it:

import cv2
import numpy as np

print(cv2.__version__)
print(np.__version__)
gout = cv2.VideoWriter('temp.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 100.0, (100, 100))
for i in range(100):
    mat = np.uint8(np.random.rand(100,100)*256)
    gout.write(mat)
gout.release()

The version of cv2 is 4.1.2 and the version of numpy is 1.17.4

I have tried different fourcc codes, and nothing is working. The video gets created, and it shows that it has data (it isn't 0kb in file explorer), but when I go to open it with VLC, the program spazzes out. The title bar flashes extrememly quickly back and forth from "temp.mp4 - VLC media player" to "VLC media player". (Other video players, like the windows Photos app don't work either)

It seems like somehow the video I am writing is corrupted, but I am really not sure how that's possible. It seems to me like this code snippet is about the simplest possible use case for cv2.VideoWriter and I can't see what part I could debug at this point.

1
  • 1
    You're trying to write grayscale images into a color video.
    – Dan Mašek
    Commented Apr 2, 2020 at 21:11

1 Answer 1

3

Thanks to Dan Masek, I realized I was writing grayscale images into a color video.

gout = cv2.VideoWriter('temp.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 100.0, (100, 100), False)

worked for me

Not the answer you're looking for? Browse other questions tagged or ask your own question.