OpenCV 4.1 VideoWriter writing green to most of avi video

I compiled and installed OpenCV 4.1 using https://github.com/mdegans/nano_build_opencv, a script which is linked in the Jetson Zoo.
I’ve been trying to process video with OpenCV on the nano, and ran into a problem. Whenever I try to write to an AVI (MJPG) video, I get a slim line of properly rendered pixels across the top of the video, while the rest is solid green. I can’t seem to find anyone with this problem online. Does anyone have an idea of why this is happening?
My code has run on non-ARM machines and worked fine. Here is what I’m running:

import cv2
import numpy as np

cap = cv2.VideoCapture("video.mp4")

frame_width = int(cap.get(3))
frame_height = int(cap.get(4))

out = cv2.VideoWriter('out.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height))
i = 0
while(True):
  ret, frame = cap.read()
 
  if ret == True: 
    out.write(frame)
    i+=1
    print(i)
    if cv2.waitKey(1) & 0xFF == ord('q'):
      break
  else:
    break

Hi,
Before installing 4.1.1, do you run [sudo apt-get purge “libopencv*”] to remove 3.3.1?

Here are steps.
Scripts for reference:
https://github.com/AastaNV/JEP/tree/master/script

Further than what @DaneLLL suggested, be also aware that opencv codec would be CPU only that may be slow depending on your resolution/framerate.
From opencv, a convenient way is having gstreamer support and use such pipeline instead (sorry it is C++, you would translate to python):

cv::VideoWriter gst_mjpg_writer("appsrc ! queue ! jpegenc ! filesink location=test-gst-writer.mjpg ", 0, fps, cv::Size(width, height));
if (!gst_mjpg_writer.isOpened()) {
	std::cout<<"Failed to open gst-mjpg-writer."<<std::endl;
	return (-4);
}
/* Can be displayed later with something like (adjust fps):
 * gst-launch-1.0 filesrc location=test-gst-writer.mjpg ! image/jpeg,format=MJPG,framerate=30/1 ! queue ! jpegparse ! jpegdec ! queue ! videoconvert ! xvimagesink */

This is CPU only but jpg decoding may be run on a different core thanks to queue.

On a Jetson you have HW encoder that can be used. You may check this and then try

cv::VideoWriter gst_nv_mjpg_writer("appsrc ! queue ! videoconvert ! video/x-raw,format=BGRx ! nvvidconv ! nvjpegenc ! queue ! filesink location=test-gst-nv-writer.mjpg ", 0, fps, cv::Size(width, height));
if (!gst_nv_mjpg_writer.isOpened()) {
	std::cout<<"Failed to open gst-nv-mjpeg writer."<<std::endl;
	return (-5);
}
/* Might also be displayed later with something like (adjust fps):
 * gst-launch-1.0 filesrc location=test-gst-nv-writer.mjpg ! image/jpeg,format=MJPG,framerate=30/1 ! queue ! jpegparse ! nvjpegdec ! nvegltransform ! nveglglessink */

[EDIT: I had edited for a working pipeline with videoconvert and nvvidconv, however nvjpegdec says with gst-inspect-1.0 it is able to handle BGR, so videoconvert and nvvidconv may not be required with different jpeg lib or gst plugins, but I cannot check now.
Someone else may try with these and tell us if this works:

cv::VideoWriter gst_nv_mjpg_writer("appsrc ! queue ! nvjpegenc ! queue ! filesink location=test-gst-nv-writer.mjpg ", 0, fps, cv::Size(width, height));

]