您的位置:首页 > 运维架构

OpenCV播放视频,并可设置进度

2017-03-06 21:22 411 查看
最近在看《Learning OpenCV》这本书,然而在讲这点时留下了一句话:

Finally, we did not include the extra tidbit of code needed to make the slider move as the video plays. This is left as an exercise for the reader.

练习。。。

果然不能愉快地偷懒了。

以下是代码,敲下来日后也好找。

#include "stdafx.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cv.hpp>
int g_slider_position = 0;//滚动条的位置
int ProgressBar_Number;//通过调用获取函数得到的当前视频的进度
CvCapture* g_capture = NULL;//视频文件结构体
//回调函数
void onTrackbarSlide(int pos) {
cvSetCaptureProperty(
g_capture,
CV_CAP_PROP_POS_FRAMES,//用帧数来设置读入位置,AVI_RATIO代替FRAMES实现按视频长度比例来设                    //置读入位置
pos
);
}
int main() {
cvNamedWindow("Video_ProgressBar", CV_WINDOW_AUTOSIZE);
g_capture = cvCreateFileCapture("[权力的游戏][第五季]第08集_bd.mp4");
int frames = (int)cvGetCaptureProperty(
g_capture,
CV_CAP_PROP_FRAME_COUNT
);
if (frames != 0) {
cvCreateTrackbar(
"ProgressBar",//滚动条的名字
"Video_ProgressBar",//窗口的名字

&g_slider_position,//滚动条的位置
frames,//总帧数
onTrackbarSlide//回调函数
);
}
IplImage* frame;
while (1)
{
frame = cvQueryFrame(g_capture);
if (!frame) break;
//获取滚动条在视频中的位置,自动更新滚动条,但在主线程中更新UI会使播放看起来比不更新滚动条要卡顿
**ProgressBar_Number = (int)cvGetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES);**
cvSetTrackbarPos("ProgressBar", "Video_ProgressBar", ProgressBar_Number);//设置进度条的位置
cvShowImage("Video_ProgressBar", frame);

char c = cvWaitKey(33);
if (c == 27) break;//ESC键
}
// Release memory and destroy window
cvReleaseCapture(&g_capture);
cvDestroyWindow("Video_ProgressBar");
return(0);
}




俺当然要支持龙母!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: