Writing RGB frames into video

Hello Everyone,

I have got a rookie question. I am receiving YUV420 frames from a picamera (YUV420 is required for acquisition speed), I convert them to RGB and then the frames are written into a video file. However the video file is not readable. Individual RGB frames can be displayed using imshow. I tried different conversion methods and video codecs. If I query RGB frames from the camera directly, videos are fine. If anyone has an idea, I would be very grateful.

import numpy as np
import cv2 
import time
from picamera2 import Picamera2


def setcam(resolutionLow,resolutionHigh):
    picam2 = Picamera2()
    preview_config = picam2.create_preview_configuration(lores={"size": resolutionLow})
    capture_config = picam2.create_still_configuration(main={"size": resolutionHigh, "format": "YUV420"})    
    return picam2,preview_config, capture_config



#some vars, to be arguments later on 
numFrames = 10
npName = 'test.csv'
videoName = 'testVideo.avi'
frameRate = 2
holdTime = 1/frameRate - 0.3
hiRes = (4056,3040)
loRes = (640,480)

#writer= cv2.VideoWriter(videoName, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), frameRate, hiRes)
writer= cv2.VideoWriter(videoName, cv2.VideoWriter_fourcc('h','2','6','4'), frameRate, hiRes)
picam2,video_config,image_config = setcam(loRes,hiRes)
picam2.configure(image_config)
picam2.start()
time.sleep(2)


counter = 0
for i in range(0,numFrames):
    rgb = np.uint8(cv2.cvtColor(picam2.capture_array(), cv2.COLOR_YUV420p2RGB))  #COLOR_YUV420p2RGB
    writer.write(rgb)
    cv2.imshow('frame', rgb)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    if counter == numFrames:
        break
    counter = counter + 1
    time.sleep(holdTime)
    



picam2.close()
writer.release()
cv2.destroyAllWindows()
print('TERMINATED')

assert writer.isOpened()

writer.isOpened() returns True. rgb has the shape (3040, 4096, 3). Could there be anything wrong with the dimensions in the rgb array?

Yes. It needs to exactly match the size used in initializing the writer, so you need to resize the images.

Thanks! Is there any information out there which codec requires what structure? Any hint what codec is the fastest (no compression needed).