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

最近在看《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);
}

这里写图片描述

俺当然要支持龙母!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值