cv2.VideoWriter doesn't work well on Jetson Nano

I’m using OpenCV v.4.1.0 on Jetson Nano to capture video with RPi camera and the module cv2.VideoWriter doesn’t work well saving video. The file is empty.

I’ve proved cv2.VideoWriter on RPi 3B+ and it worked fine.

import numpy as np
import cv2

capture_height = 720
capture_width = 1280
frame_rate = 21
display_width = 860
display_height = 640
flip_method = 0

gstr = ('nvarguscamerasrc ! video/x-raw(memory:NVMM),'
        'width=%s, height=%s,'
        'framerate= %s'
        'format=NV12 ! nvvidconv flip-method= %s ! video/x-raw,'   
        'width=%s, height=%s,'
        'format=BGRx ! videoconvert ! appsink'
        % (capture_width, capture_height, frame_rate, flip_method, 
        display_width, display_height))

filename = 'video.avi'
fourcc = cv2.VideoWriter_fourcc(*'XVID')

cap = cv2.VideoCapture(gstr, cv2.CAP_GSTREAMER)
out = cv2.VideoWriter(filename, fourcc, float(frame_rate), 
                     (capture_width,capture_height),True)

while True:
    ret, img = cap.read()
    out.write(img)
    cv2.imshow('img',img)
    if cv2.waitKey(1) & 0xff == ord('q'):
        break

cap.release()
out.release()
cv2.destroyAllWindows()

This code should show an image from RPi camera, and save a video with .avi format, and I already proved using other codecs, but still nothing.

Hi,
Does gst-launch-1.0 work?

$ gst-launch-1.0 nvarguscamerasrc ! 'video/x-raw(memory:NVMM),format=NV12,width=1280,height=720,framerate=21/1' ! nvvidconv ! video/x-raw,format=BGRx,width=864,height=640 ! videoconvert ! video/x-raw,format=BGR ! appsink

And it seems to miss one comma in the line:

'framerate= %s<b>,</b>'

Yep, it does.
And about the code, without that comma it shows and image anyway, but the file saved is empty.
Also the program recognizes the codec:

>>> cv2.VideoWriter_fourcc(*'XVID') 
1145656920

Hi,
Does it work to save an mp4?
[url]https://devtalk.nvidia.com/default/topic/1029451/jetson-tx2/-python-what-is-the-four-characters-fourcc-code-for-mp4-encoding-on-tx2/post/5237323/#5237323[/url]

I already saw that post and tried the codes, but I had the same result.

Hi,
Found below link and it seems you should set display_widthxdisplay_height(860x640) to VideoWriter.
[url]Writing an mp4 video using python opencv - Stack Overflow

Wow, it really works.
Thanks!

1 Like