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. Capture a frame (image) from a video playing in a QT GUI

Capture a frame (image) from a video playing in a QT GUI

Scheduled Pinned Locked Moved Solved General and Desktop
videoframec++qt 6
7 Posts 3 Posters 2.2k Views
  • 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 Offline
    B Offline
    BigBen
    wrote on 3 May 2022, 12:03 last edited by BigBen 5 Apr 2022, 02:35
    #1

    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();
    }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 3 May 2022, 19:13 last edited by
      #2

      Hi and welcome to devnet,

      Which examples are you following ? You did not provide the links in your post.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • B Offline
        B Offline
        BigBen
        wrote on 4 May 2022, 02:27 last edited by
        #3

        @SGaist Sorry about that. These are the links I wanted to provide:

        link1
        link2

        J 1 Reply Last reply 4 May 2022, 05:14
        0
        • B BigBen
          4 May 2022, 02:27

          @SGaist Sorry about that. These are the links I wanted to provide:

          link1
          link2

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 4 May 2022, 05:14 last edited by
          #4

          @BigBen And what is the problem following this example? You already have a QMediaPlayer. Implement a class subclassing QAbstractVideoSurface and set it in the player as shown in the example.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • B Offline
            B Offline
            BigBen
            wrote on 5 May 2022, 08:37 last edited by BigBen 5 May 2022, 08:58
            #5

            @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.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BigBen
              wrote on 5 May 2022, 09:20 last edited by
              #6

              @jsulm Also, just realized that I am using Qt 6.3, in which QAbstractVideoSurface is not there anymore. I think I will have to use QVideoSink in some way. Would be grateful if you can guide me as to how I can use that.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 5 May 2022, 19:28 last edited by
                #7

                Connect the videoFrameChanged signal of the sink to a custom slot so you will have the frame available to be saved.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                7/7

                5 May 2022, 19:28

                • Login

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