问题
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)?
回答1:
Below steps may help you to capture a frame from a video file.
Project level
- QT += multimedia
Code level
- Initiate QMediaplayer object (QMediaPlayer(QObject parent, QMediaPlayer::VideoSurface)
- set QMediaplayer.setVideoOutput to(Subclass of QAbstractVideoSurface)
- 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 - Load the video file using QMediaplayer
- setMute = true(audio)
- Set the needed position in milliseconds to the QMediaplayer object
- Start play method
- From the present method convert the received data buffer to QImage and then to QPixmap(if needed).
- Once got the pixmap, use it to load in a widget (example: In a QLabel)
- 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
- 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)
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
来源:https://stackoverflow.com/questions/38468644/getting-video-frame-in-provided-time-qt