from QCamera or QVideoWidget get video bytes
-
wrote on 9 Oct 2022, 03:32 last edited by
Hi, How can I get ARRAY OF BYTES (frame) from QCamera or QVideoWidget? I successfull capture video from camera and audio from microphone and now I want send they to socket tcp channel for creating a simple video audio call software.
-
Hi,
Depending on your platform, you could use QVideoProbe.
That said, dumping the raw content of your camera on the network is not a good idea, you are going to kill your bandwidth. There are libraries around that are designed for that kind of activities, you should check them.
-
wrote on 10 Oct 2022, 06:01 last edited by
Ok, with QVideoProbe I have access to QVideoFrame. QVideoFrame bits() method return ( char * ). I want array of bytes, How Can I do? casting char * in QByteArray() ?
-
Ok, with QVideoProbe I have access to QVideoFrame. QVideoFrame bits() method return ( char * ). I want array of bytes, How Can I do? casting char * in QByteArray() ?
@zabitqt said in from QCamera or QVideoWidget get video bytes:
I want array of bytes
char* is basically an array of bytes in C/C++.
If you want QByteArray then use https://doc.qt.io/qt-6/qbytearray.html#QByteArray-1 -
wrote on 10 Oct 2022, 07:55 last edited by
Since some of the bytes in the video frame can be zero you will need to pass the size of the char* buffer to the QByteArray constructor @jsulm linked. See QVideoFrame::mappedBytes().
-
@zabitqt said in from QCamera or QVideoWidget get video bytes:
I want array of bytes
char* is basically an array of bytes in C/C++.
If you want QByteArray then use https://doc.qt.io/qt-6/qbytearray.html#QByteArray-1 -
wrote on 10 Oct 2022, 10:30 last edited by
QVideoProbe* probe = new QVideoProbe;
qDebug() << "lo stato di probe";
qDebug() << probe->setSource(recorder);
QObject::connect(probe, SIGNAL(videoFrameProbed(QVideoFrame)), this, SLOT(processFrame(QVideoFrame)));The function probe->setSource(recorder); return always false. Why?
-
QVideoProbe* probe = new QVideoProbe;
qDebug() << "lo stato di probe";
qDebug() << probe->setSource(recorder);
QObject::connect(probe, SIGNAL(videoFrameProbed(QVideoFrame)), this, SLOT(processFrame(QVideoFrame)));The function probe->setSource(recorder); return always false. Why?
@zabitqt said in from QCamera or QVideoWidget get video bytes:
qDebug() << probe->setSource(recorder);
What exactly is recorder here and was it initialised?
-
@zabitqt said in from QCamera or QVideoWidget get video bytes:
qDebug() << probe->setSource(recorder);
What exactly is recorder here and was it initialised?
wrote on 10 Oct 2022, 11:36 last edited byQCamera* mCamera = new QCamera(); QMediaRecorder* recorder = new QMediaRecorder(mCamera); QVideoEncoderSettings settings = recorder->videoSettings(); 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); recorder->record(); mCamera->start();
Could be that If the media recorder instance does not support monitoring video, this function will return false?
-
QCamera* mCamera = new QCamera(); QMediaRecorder* recorder = new QMediaRecorder(mCamera); QVideoEncoderSettings settings = recorder->videoSettings(); 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); recorder->record(); mCamera->start();
Could be that If the media recorder instance does not support monitoring video, this function will return false?
@zabitqt said in from QCamera or QVideoWidget get video bytes:
Could be that If the media recorder instance does not support monitoring video, this function will return false?
According to documentation: "If the media recorder instance does not support monitoring video, this function will return false"
-
wrote on 10 Oct 2022, 12:40 last edited by
Ok but Is there an alternative? It's impossible I can't get bytes from camera and send via QTcpSocket.
Help me please
-
wrote on 11 Oct 2022, 06:24 last edited by 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. -
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.wrote on 14 Oct 2022, 06:16 last edited by@Bonnie hi, I also ask to stackoverflow
https://stackoverflow.com/questions/74058405/capture-qvideoframe-and-display-it-in-qvideowidget
Can you help me
-
wrote on 14 Oct 2022, 06:26 last edited by Bonnie
I don't know much about
QAbstractVideoSurface
.
Did you try usingQVideoProbe
to monitor theQCamera
object as I said?
If that works then there's no need to create your custom video surface to get frames. -
wrote on 14 Oct 2022, 07:20 last edited by
@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);
-
@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);
-
wrote on 14 Oct 2022, 07:27 last edited by
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
-
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
-
wrote on 14 Oct 2022, 07:37 last edited by
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(); } }
-
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(); } }
wrote on 14 Oct 2022, 07:46 last edited by 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 callframe.image()
, you don't even need to clone, map and unmap...
7/22