Getting video frame in provided time Qt

你。 提交于 2019-12-13 16:34:42

问题


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

  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)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!