Save video with OpenCV and usb-webcamera

Hello, I have a new web-camera with this parameters

ioctl: VIDIOC_ENUM_FMT
	Index       : 0
	Type        : Video Capture
	Pixel Format: 'MJPG' (compressed)
	Name        : Motion-JPEG
		Size: Discrete 1920x1080
			Interval: Discrete 0.033s (30.000 fps)
		Size: Discrete 1280x720
			Interval: Discrete 0.033s (30.000 fps)
		Size: Discrete 800x480
			Interval: Discrete 0.033s (30.000 fps)
		Size: Discrete 640x480
			Interval: Discrete 0.033s (30.000 fps)
		Size: Discrete 640x360
			Interval: Discrete 0.033s (30.000 fps)
		Size: Discrete 320x240
			Interval: Discrete 0.033s (30.000 fps)
		Size: Discrete 176x144
			Interval: Discrete 0.033s (30.000 fps)
		Size: Discrete 800x600
			Interval: Discrete 0.033s (30.000 fps)
		Size: Discrete 1920x1080
			Interval: Discrete 0.033s (30.000 fps)

	Index       : 1
	Type        : Video Capture
	Pixel Format: 'YUYV'
	Name        : YUYV 4:2:2
		Size: Discrete 640x480
			Interval: Discrete 0.033s (30.000 fps)
		Size: Discrete 640x360
			Interval: Discrete 0.033s (30.000 fps)
		Size: Discrete 320x240
			Interval: Discrete 0.033s (30.000 fps)
		Size: Discrete 176x144
			Interval: Discrete 0.033s (30.000 fps)
		Size: Discrete 640x480
			Interval: Discrete 0.033s (30.000 fps)

Based on this discussion
I was able to show video in full-hd with this code bellow.

cap = cv2.VideoCapture(‘v4l2src device=/dev/video0 io-mode=2 ! image/jpeg, width=(int)1920, height=(int)1080, framerate=30/1 ! nvv4l2decoder mjpeg=1 ! nvvidconv ! video/x-raw,format=BGRx ! videoconvert ! video/x-raw, format=BGR ! appsink’, cv2.CAP_GSTREAMER)

I tried also this one (without convertion)

cap = cv2.VideoCapture(‘v4l2src device=/dev/video0 io-mode=2 ! image/jpeg, width=(int)1920, height=(int)1080 ! nvjpegdec ! video/x-raw ! videoconvert ! video/x-raw,format=BGR ! appsink’, cv2.CAP_GSTREAMER)

but with this one it was black and white.

So I’m trying to use this code


    cap = cv2.VideoCapture ('v4l2src device=/dev/video0 io-mode=2 ! image/jpeg, width=(int)1920, height=(int)1080, framerate=30/1 ! nvv4l2decoder mjpeg=1 ! nvvidconv ! video/x-raw,format=BGRx ! videoconvert ! video/x-raw, format=BGR ! appsink', cv2.CAP_GSTREAMER)
                           
    fourcc = cv2.VideoWriter_fourcc(*'MPEG')
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    fps = cap.get(cv2.CAP_PROP_FPS)
    print("fourcc:{} fps:{}@width:{}@height:{}".format(fourcc, fps, width, height))
    file_name = "abc.avi"
    out = cv2.VideoWriter(file_name, fourcc, 30, (1920, 1088))
    counter = 0
    while True:
        if counter > 200:
            break
        counter +=1

        _, frame = cap.read()
        if(frame is None):
            continue


        # cv2.imshow('frame', frame)
        
        out.write(frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        
        

    cap.release()
    cv2.destroyAllWindows()

I’m getting this output

Opening in BLOCKING MODE 
NvMMLiteOpen : Block : BlockType = 277 
NVMEDIA: Reading vendor.tegra.display-size : status: 6 
NvMMLiteBlockCreate : Block : BlockType = 277 
[ WARN:0] global /home/suomi/opencv/modules/videoio/src/cap_gstreamer.cpp (961) open OpenCV | GStreamer warning: Cannot query video position: status=1, value=6, duration=-1
fourcc:1195724877 fps:30.0@width:1920.0@height:1088.0

but the final video is strange… it shows 1920x1080 30fps in video properties but the video is like lagging or so. Just 4 frames in the whole video

Btw. I don’t know why, but in cv2.CAP_PROP_FRAME_HEIGHT parameter I’m becoming 1088.
Could someone please explain me how to set the VideoWritter right?

Update: I was also able to run camera with

$ export DISPLAY=:0 (or DISPLAY=:1)
$ gst-launch-1.0 v4l2src device=/dev/video0 io-mode=2 ! image/jpeg, width=(int)1920, height=(int)1080 !  nvjpegdec ! video/x-raw ! xvimagesink

from the code in hyperlink above. But I’m not able to set this in opencv

cap = cv2.VideoCapture ('v4l2src device=/dev/video0 io-mode=2 ! image/jpeg, width=1920, height=1080 !  nvjpegdec ! video/x-raw ! xvimagesink', cv2.CAP_GSTREAMER)

shows empty pipeline

[ WARN:0] global /home/suomi/opencv/modules/videoio/src/cap_gstreamer.cpp (824) open OpenCV | GStreamer warning: cannot find appsink in manual pipeline
[ WARN:0] global /home/suomi/opencv/modules/videoio/src/cap_gstreamer.cpp (501) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
fourcc:1195724877 fps:0.0@width:0.0@height:0.0

I’m also able to run this pipeline in terminal

'v4l2src device=/dev/video0 io-mode=2 ! image/jpeg, width=(int)1920, height=(int)1080 !  nvjpegdec ! video/x-raw ! videoconvert ! video/x-raw,format=BGR ! appsink', cv2.CAP_GSTREAMER

but in opencv it runs but frames are black and nothing more

Hi,
There is a sample of demonstrating cv2.VideoWriter():

Please take a look.