本文介绍了在规定的时间Qt中获取视频帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做的是在某个时间(例如20秒)获取视频帧.我知道我可以做这样的事情-倒带视频并暂停它:
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();
但是有没有更优雅的解决方案(对我来说这似乎是个hack,因为我不需要整个视频,但有时只需要一帧)?
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)?
推荐答案
以下步骤可以帮助您从视频文件中捕获帧.
项目级别
- QT + =多媒体
代码级别
- 启动QMediaplayer对象(QMediaPlayer(QObject父级,QMediaPlayer :: VideoSurface)
- 将QMediaplayer.setVideoOutput设置为(QAbstractVideoSurface的子类)
- QAbstractVideoSurface的子类应重新实现受支持的方法:PixelFormats,isFormatSupported,开始,存在
4.通过 present 方法,我们可以获取每一帧的图像缓冲区 - 使用QMediaplayer加载视频文件
- setMute = true(音频)
- 将所需位置(以毫秒为单位)设置为QMediaplayer对象
- 开始播放方法
- 从 present 方法将接收到的数据缓冲区转换为QImage,然后转换为QPixmap(如果需要).
- 一旦获得了像素图,就可以使用它加载到小部件中(例如:在QLabel中)
- 立即暂停视频文件的播放(如果您需要捕获其他帧.其他明智的选择stop()而不是pause()).可以使用从子类的对象(QAbstractVideoSurface)到QMediaPlayer对象的信号槽
- 完成后,调用QAbstractVideoSurface子类的stop方法,然后调用QMediaplayer
- 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
(应用程序屏幕截图)
打开视频文件:浏览并选择一个视频文件
滑块:选择所需的位置
捕获:捕获图像并在QLabel中查看
保存:保存捕获的图像
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
这篇关于在规定的时间Qt中获取视频帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!