Capture a frame (image) from a video playing in a QT GUI
-
I have written a simple video player GUI code in QT. The GUI allows the user to browse the local files and select a video for playing in the GUI. The GUI also has options for 'play', 'pause' and 'stop' to apply to the video selected.
I want to add another button 'Capture', that captures the current frame of the video that is being played, and displays this captured image next to the video (The video should should get paused at this point). I looked into the documentation of QT, specifically: this and this. But I am still not able to understand how to implement this in my case.
Kindly guide.
My code so far is as follows:
#include "qtwidgetsapplication4.h" #include <iostream> QtWidgetsApplication4::QtWidgetsApplication4(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); player = new QMediaPlayer(this); vw = new QVideoWidget(this); player->setVideoOutput(vw); this->setCentralWidget(vw); } void QtWidgetsApplication4::on_actionOpen_triggered() { QString filename = QFileDialog::getOpenFileName(this, "Open a File", "", "Video File (*.*)"); on_actionStop_triggered(); player->setSource(QUrl::fromLocalFile(filename)); on_actionPlay_triggered(); qDebug("Error Message in actionOpen"); qDebug()<<player->mediaStatus(); } void QtWidgetsApplication4::on_actionPlay_triggered() { player->play(); ui.statusBar->showMessage("Playing"); qDebug("Error Message in actionPlay"); qDebug() << player->mediaStatus(); } void QtWidgetsApplication4::on_actionPause_triggered() { player->pause(); ui.statusBar->showMessage("Paused..."); qDebug("Error Message in actionPause"); qDebug() << player->mediaStatus(); } void QtWidgetsApplication4::on_actionStop_triggered() { player->stop(); ui.statusBar->showMessage("Stopped"); qDebug("Error Message in actionStop"); qDebug() << player->mediaStatus(); }
-
Hi and welcome to devnet,
Which examples are you following ? You did not provide the links in your post.
-
@jsulm Apologies, but being a beginner I find it a bit difficult to understand how to integrate different concepts straight away from the documentation. You'll understand that from my doubts below:
The confusion I have is that, do I need to subclass my main class (QtWidgetsApplication4) itself?
And after I implement a class subclassing QAbstractVideoSurface, and set it in the player as shown, what do I do with the current setting of
player->setVideoOutput(vw);
?My next confusion is that how do I integrate all this with the button I want to implement to capture the current frame?
The QMediaPlayer I already have is for playing the videos.
I am just asking for an explanation regarding how all this is linked and can be implemented from the point where I am currently at.
-
Connect the videoFrameChanged signal of the sink to a custom slot so you will have the frame available to be saved.