1

I have tried to save a list of arrays to a video using the following code, but it isn't working.

out = cv2.VideoWriter("output.mp4", cv2.VideoWriter_fourcc(*'mp4v'), 30, (1280, 720))
for frame in frames:
    out.write(frame) # frame is a numpy.ndarray with shape (1280, 720, 3)
out.release()

It fails with no error message, but the output file (output.mp4) is around 200 bytes long and doesn't open in QuickTime or VLC. My assumption is that out.write is somehow failing silently. Is it possible to write arrays to a video in this way? If not, how could it be done?

Any and all help is very much appreciated.

5
  • Can you check your frame size? Most probably the creates the problem Commented May 24, 2020 at 4:26
  • I believe the frame size is correct, but I will double check. Thanks. Commented May 24, 2020 at 4:30
  • Try this out = cv2.VideoWriter("output.mp4", cv2.VideoWriter_fourcc(*'mp4v'), 30, (720, 1080)) Commented May 24, 2020 at 4:35
  • It would seem the x and y axis were switched in the numpy array. Thanks for your help. Commented May 24, 2020 at 4:39
  • You are welcome. I made a formal answer below Commented May 24, 2020 at 4:40

1 Answer 1

3

Numpy array is (row,column) but OpenCV defines images by (width,height). So, in your numpy array height=row=1080 and width=column=720. So, Change the frame size (1080,720) to (720,1080).

out = cv2.VideoWriter("output.mp4", cv2.VideoWriter_fourcc(*'mp4v'), 30, (720, 1080))
for frame in frames:
    out.write(frame) # frame is a numpy.ndarray with shape (1280, 720, 3)
out.release()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.