为 USB 相机 MJPG 格式的 GStreamer 创建管道

问题描述

我有 Jetson Nano,我想从我的 usb-camera 中保存视频。 我的相机输出 MJPG 有问题 - 它输出的是 YUYV 而不是 MJPG,但我能够通过 io-mode=2pipeline 设置解决它/p>

这适用于我的 Opencv (4.5.1)

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()

但是视频太滞后了,因为转换?,它打印 30fps,但它为 10 秒视频保存了 4 帧。它也从 1088 获得 CAP_PROP_FRAME_HEIGHT,知道为什么...

我发现这个 pipeline 可以在终端中没有 lags 的情况下工作

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

但我无法在 opencv

中使用它

它输出空的 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

我也试过这个,它在终端中工作,但是当我像这样使用 opencv 时显示黑框

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

我的相机设置在这里


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)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)