I want to save an image of a frame from a QMediaPlayer
. After reading the documentation, I understood that I should use QVideoProbe
. I am using the following code :
QMediaPlayer *player = new QMediaPlayer();
QVideoProbe *probe = new QVideoProbe;
connect(probe, SIGNAL(videoFrameProbed(QVideoFrame)), this, SLOT(processFrame(QVideoFrame)));
qDebug()<<probe->setSource(player); // Returns true, hopefully.
player->setVideoOutput(myVideoSurface);
player->setMedia(QUrl::fromLocalFile("observation.mp4"));
player->play(); // Start receving frames as they get presented to myVideoSurface
But unfortunately, probe->setSource(player)
always returns false
for me, and thus my slot processFrame
is not triggered.
What am I doing wrong ? Does anybody have a working example of QVideoProbe
?
You're not doing anything wrong. As @DYangu pointed out, your media object instance does not support monitoring video. I had the same problem (and same for
QAudioProbe
but it doesn't interest us here). I found a solution by looking at this answer and this one.The main idea is to subclass QAbstractVideoSurface. Once you've done that, it will call the method
QAbstractVideoSurface::present(const QVideoFrame & frame)
of your implementation ofQAbstractVideoSurface
and you will be able to process the frames of your video.As it is said here, usually you will just need to reimplement two methods :
QVideoFrame
But at the time, I searched in the Qt source code and happily found this piece of code which helped me to do a full implementation. So, here is the full code for using a "video frame grabber".
VideoFrameGrabber.cpp :
VideoFrameGrabber.h
Note : in the .h, you will see I added a signal taking an image as a parameter. This will allow you to process your frame anywhere in your code. At the time, this signal took a
QImage
as a parameter, but you can of course take aQVideoFrame
if you want to.Now, we are ready to use this video frame grabber:
Now you just have to declare a slot named
processFrame(QImage image)
and you will receive aQImage
each time you will enter the method present of yourVideoFrameGrabber
.I hope that this will help you!
After Qt QVideoProbe documentation:
So it seems your "media object instance does not support monitoring video"