Below are the steps to help you capture a frame from a video file.
Project level
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)

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.
source share