Receiving a video clip in the stipulated time Qt

What I want to do is get the video frame after a while (for example, after 20 seconds). I know that I can do something like this - rewind the video and pause it:

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

But is there an even more elegant solution (it seems to me a hack, because I do not need the whole video, but only a frame at some time)?

+2
source share
1 answer

Below are the steps to help you capture a frame from a video file.

Project level

  • QT + = multimedia

Code level

  • Initiate a QMediaplayer object (QMediaPlayer (parent QObject, QMediaPlayer :: VideoSurface)
  • set QMediaplayer.setVideoOutput to (subclass of QAbstractVideoSurface)
  • The subclass QAbstractVideoSurface should repeat the implementation of the supported methods. PixelFormats, isFormatSupported, start, present
    4. From the method of the present, we can get an image buffer for each frame
  • Upload a video using QMediaplayer
  • setMute = true (audio)
  • Set the desired position in milliseconds to the QMediaplayer object.
  • Start play method
  • From this method, convert the resulting data buffer to QImage, and then to QPixmap (if necessary).
  • As soon as pixmap comes out, use it to load into the widget (example: in QLabel)
  • Pause the video file immediately (if you need to take some other frame). Another wise stop () instead of a pause ()). This can be done using the signal slot from the subclass object (QAbstractVideoSurface) to the QMediaPlayer object
  • When done, call the QAbstractVideoSurface subclass stop method, and then QMediaplayer

The above sample application can be found here

(Application screen screen)

enter image description here

Open video file: view and select a video file

Slider: select the desired position
Capture: capture images and view in QLabel
Save: Save the captured image.

+3
source

Source: https://habr.com/ru/post/988999/


All Articles