Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. from QCamera or QVideoWidget get video bytes
Forum Updated to NodeBB v4.3 + New Features

from QCamera or QVideoWidget get video bytes

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 5 Posters 2.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Bonnie

    Take a look at https://wiki.qt.io/Qt_5.13_Multimedia_Backends, QVideoProbe needs the backend to support "Video probe" feature.
    As listed in the tables, recording of Windows and Unix doesn't support it, but player and camera (except wmf) do support.
    So I think it may be possible to monitor the camera object directly, but you'll need to handle the media encoding yourself.

    Z Offline
    Z Offline
    zabitqt
    wrote on last edited by
    #13

    @Bonnie hi, I also ask to stackoverflow

    https://stackoverflow.com/questions/74058405/capture-qvideoframe-and-display-it-in-qvideowidget

    Can you help me

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #14

      I don't know much about QAbstractVideoSurface.
      Did you try using QVideoProbe to monitor the QCamera object as I said?
      If that works then there's no need to create your custom video surface to get frames.

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zabitqt
        wrote on last edited by
        #15

        @Bonnie said in from QCamera or QVideoWidget get video bytes:

        QVideoProbe

        I test QVideoProbe but unfortunately return false
        qDebug() << "return" << prob->setSource(recorder);

        QCamera* mCamera = new QCamera();
        QMediaRecorder* recorder = new QMediaRecorder(mCamera);
        QVideoEncoderSettings settings = recorder->videoSettings();
        QVideoProbe* prob = new QVideoProbe();
        
        settings.setResolution(640, 480);
        settings.setQuality(QMultimedia::VeryHighQuality);
        settings.setFrameRate(30.0);
        settings.setCodec("video/mp4");
        recorder->setVideoSettings(settings);
        recorder->setContainerFormat("mp4");
        mCamera->setCaptureMode(QCamera::CaptureVideo);
        
        B 1 Reply Last reply
        0
        • Z zabitqt

          @Bonnie said in from QCamera or QVideoWidget get video bytes:

          QVideoProbe

          I test QVideoProbe but unfortunately return false
          qDebug() << "return" << prob->setSource(recorder);

          QCamera* mCamera = new QCamera();
          QMediaRecorder* recorder = new QMediaRecorder(mCamera);
          QVideoEncoderSettings settings = recorder->videoSettings();
          QVideoProbe* prob = new QVideoProbe();
          
          settings.setResolution(640, 480);
          settings.setQuality(QMultimedia::VeryHighQuality);
          settings.setFrameRate(30.0);
          settings.setCodec("video/mp4");
          recorder->setVideoSettings(settings);
          recorder->setContainerFormat("mp4");
          mCamera->setCaptureMode(QCamera::CaptureVideo);
          
          B Offline
          B Offline
          Bonnie
          wrote on last edited by Bonnie
          #16

          @zabitqt Haven't you read my above reply? I said to monitor the camera object, not the recorder, because that's unsupported on Windows and Unix...

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            zabitqt
            wrote on last edited by
            #17

            Ok now return true!

            but it display
            QVideoFrame(QSize(640, 480), Format_YUYV, NoHandle, ReadOnly, [no timestamp])
            image base64: ""

            I don't understand. I can pay you for consulting If you resolve me problem

            B 1 Reply Last reply
            0
            • Z zabitqt

              Ok now return true!

              but it display
              QVideoFrame(QSize(640, 480), Format_YUYV, NoHandle, ReadOnly, [no timestamp])
              image base64: ""

              I don't understand. I can pay you for consulting If you resolve me problem

              B Offline
              B Offline
              Bonnie
              wrote on last edited by
              #18

              @zabitqt I don't do consulting jobs haha. What do you not understand? How to get bytes from QVideoFrame?

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                zabitqt
                wrote on last edited by
                #19
                void QtVideoWidgetsIssueTrack::processFrame(const QVideoFrame& frame) {
                    qDebug() << "processFrame";
                    if (frame.isValid()) {
                        QVideoFrame cloneFrame(frame);
                
                        cloneFrame.map(QAbstractVideoBuffer::ReadOnly);
                        qDebug() << cloneFrame;
                
                        const QImage image(cloneFrame.bits(),
                            cloneFrame.width(),
                            cloneFrame.height(),
                            QVideoFrame::imageFormatFromPixelFormat(cloneFrame.pixelFormat()));
                
                        QByteArray ba;
                        QBuffer bu(&ba);
                        bu.open(QBuffer::ReadWrite);
                        //bu.open(QIODevice::WriteOnly);
                        image.save(&bu, "PNG");
                        //bu.close();
                        //QString imgBase64 = ba.toBase64();
                        QString imgBase64 = QString::fromLatin1(ba.toBase64().data());
                        qDebug() << "image base64: " << imgBase64;
                
                        cloneFrame.unmap();
                    }
                }
                
                B 1 Reply Last reply
                0
                • Z zabitqt
                  void QtVideoWidgetsIssueTrack::processFrame(const QVideoFrame& frame) {
                      qDebug() << "processFrame";
                      if (frame.isValid()) {
                          QVideoFrame cloneFrame(frame);
                  
                          cloneFrame.map(QAbstractVideoBuffer::ReadOnly);
                          qDebug() << cloneFrame;
                  
                          const QImage image(cloneFrame.bits(),
                              cloneFrame.width(),
                              cloneFrame.height(),
                              QVideoFrame::imageFormatFromPixelFormat(cloneFrame.pixelFormat()));
                  
                          QByteArray ba;
                          QBuffer bu(&ba);
                          bu.open(QBuffer::ReadWrite);
                          //bu.open(QIODevice::WriteOnly);
                          image.save(&bu, "PNG");
                          //bu.close();
                          //QString imgBase64 = ba.toBase64();
                          QString imgBase64 = QString::fromLatin1(ba.toBase64().data());
                          qDebug() << "image base64: " << imgBase64;
                  
                          cloneFrame.unmap();
                      }
                  }
                  
                  B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by Bonnie
                  #20

                  @zabitqt Oh, you can't convert it to QImage like this, as your debug info shows, the frame data you get from the camera is in YUV format, which QImage doesn't support.
                  If you want to get a QImage, just call frame.image(), you don't even need to clone, map and unmap...

                  Z 1 Reply Last reply
                  2
                  • B Bonnie

                    @zabitqt Oh, you can't convert it to QImage like this, as your debug info shows, the frame data you get from the camera is in YUV format, which QImage doesn't support.
                    If you want to get a QImage, just call frame.image(), you don't even need to clone, map and unmap...

                    Z Offline
                    Z Offline
                    zabitqt
                    wrote on last edited by
                    #21

                    @Bonnie said in from QCamera or QVideoWidget get video bytes:

                    frame.image()

                    It doesn't exist method frame.image(). OK I have understand that the problem is QImage that not support Format_YUYV. I search a solution in google. Thank you so much!

                    B 1 Reply Last reply
                    0
                    • Z zabitqt

                      @Bonnie said in from QCamera or QVideoWidget get video bytes:

                      frame.image()

                      It doesn't exist method frame.image(). OK I have understand that the problem is QImage that not support Format_YUYV. I search a solution in google. Thank you so much!

                      B Offline
                      B Offline
                      Bonnie
                      wrote on last edited by Bonnie
                      #22

                      @zabitqt This function exists since qt5.15 :), in which Qt has done the YUV to RGB conversion internally.
                      (Also have checked that in Qt6 it should be toImage() )

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved