c++qtvideoqtmultimediaqt5.7

Getting video frame in provided time Qt


What I want to do is to get video frame at some time (for example at 20 sec). I know I could do something like this - rewind video and pause it:

QMediaPlayer* player = new QMediaPlayer;
...
player->play();
player->setPosition(20000);
player->pause();

But is there some more elegant solution (this seems like a hack to me since I don't need whole video but only a frame at some time)?


Solution

  • Below steps may help you to capture a frame from a video file.

    Project level

    1. QT += multimedia

    Code level

    1. Initiate QMediaplayer object (QMediaPlayer(QObject parent, QMediaPlayer::VideoSurface)
    2. set QMediaplayer.setVideoOutput to(Subclass of QAbstractVideoSurface)
    3. Subclass of QAbstractVideoSurface should re-implement the methods supportedPixelFormats, isFormatSupported, start, present
      4.From the present method we can get the image buffer of each frame
    4. Load the video file using QMediaplayer
    5. setMute = true(audio)
    6. Set the needed position in milliseconds to the QMediaplayer object
    7. Start play method
    8. From the present method convert the received data buffer to QImage and then to QPixmap(if needed).
    9. Once got the pixmap, use it to load in a widget (example: In a QLabel)
    10. Immediately pause the video file from playing (if you need to capture some other frame. Other wise stop() instead of pause()). This can be done using signal-slot from subclass's object (QAbstractVideoSurface) to QMediaPlayer object
    11. When done,call the stop method of sub class of QAbstractVideoSurface and then the QMediaplayer

    Above said example application can be found here

    (Application Screen Shot)

    enter image description here

    Open Video File : Browse and select a video file
    Slider : select the position you want
    Capture : capture the image and view in a QLabel
    Save : Save the captured image