(代码4)简单视频控制

来源:互联网 发布:mac上的解压缩软件 编辑:程序博客网 时间:2024/05/21 21:37
#include <stdio.h>
#include <iostream>
#include <fstream>
#include "cv.h"
#include "highgui.h"
using namespace std;

int        g_slider_position = 0;
CvCapture* g_capture         = NULL;

void onTrackbarSlide(int pos) {
    cvSetCaptureProperty(
        g_capture,
        CV_CAP_PROP_POS_FRAMES,
        pos
    );
}

//Hack because sometimes the number of frames in a video is not accessible. 
//Probably delete this on Widows
int getAVIFrames(char * fname) { 
    char tempSize[4];
    // Trying to open the video file
    ifstream  videoFile( fname , ios::in | ios::binary );
    // Checking the availablity of the file
    if ( !videoFile ) {
      cout << "Couldn’t open the input file " << fname << endl;
      exit( 1 );
    }
    // get the number of frames
    videoFile.seekg( 0x30 , ios::beg );
    videoFile.read( tempSize , 4 );
    int frames = (unsigned char ) tempSize[0] + 0x100*(unsigned char ) tempSize[1] + 0x10000*(unsigned char ) tempSize[2] +    0x1000000*(unsigned char ) tempSize[3];
    videoFile.close(  );
    return frames;
}


int main( int argc, char** argv ) {
    cvNamedWindow( "Example2_3", CV_WINDOW_AUTOSIZE );
    g_capture = cvCreateFileCapture( argv[1] );
    IplImage *foo = cvQueryFrame( g_capture);

    int frames = (int) cvGetCaptureProperty(
        g_capture,
        CV_CAP_PROP_FRAME_COUNT
    );
    
    int tmpw = (int) cvGetCaptureProperty(
        g_capture,
        CV_CAP_PROP_FRAME_WIDTH
    );


    int tmph = (int) cvGetCaptureProperty(
        g_capture,
        CV_CAP_PROP_FRAME_HEIGHT
    );


    printf("opencv frames %d w %d h %d\n",frames,tmpw,tmph);

    frames = getAVIFrames(argv[1]); //This is a hack because on linux, getting number of frames often doesn't work


    printf("hacked frames %d w %d h %d\n",frames,tmpw,tmph);


    cvCreateTrackbar(
        "Position",
        "Example2_3",
        &g_slider_position,
        frames,
        onTrackbarSlide
    );
    IplImage* frame;
    frames = 0;
    while(1) {
        frame = cvQueryFrame( g_capture );
        if( !frame ) break;
//      int frames = cvGetCaptureProperty( g_capture, CV_CAP_PROP_POS_FRAMES);//This should work, sometimes it does not on linux
frames++; //My cheat
printf("\nFrame number=%d\n",frames);
        cvSetTrackbarPos("Position","Example2_3",frames);
        cvShowImage( "Example2_3", frame );
        char c = (char)cvWaitKey(10);
        if( c == 27 ) break;
    }
    cvReleaseCapture( &g_capture );
    cvDestroyWindow( "Example2_3" );
    return(0);

}

cvSetCaptureProperty

设置视频获取属性

int cvSetCaptureProperty( CvCapture* capture, int property_id, double value );
capture 
视频获取结构。
property_id 
属性标识符。可以是下面之一:
CV_CAP_PROP_POS_MSEC - 从文件开始的位置,单位为毫秒
CV_CAP_PROP_POS_FRAMES - 单位为帧数的位置(只对视频文件有效)
CV_CAP_PROP_POS_AVI_RATIO - 视频文件的相对位置(0 - 影片的开始,1 - 影片的结尾)
CV_CAP_PROP_FRAME_WIDTH - 视频流的帧宽度(只对摄像头有效)
CV_CAP_PROP_FRAME_HEIGHT - 视频流的帧高度(只对摄像头有效)
CV_CAP_PROP_FPS - 帧率(只对摄像头有效)
CV_CAP_PROP_FOURCC - 表示codec的四个字符(只对摄像头有效)
value 
属性的值。

函数cvSetCaptureProperty设置指定视频获取的属性。目前这个函数对视频文件只支持: CV_CAP_PROP_POS_MSEC, CV_CAP_PROP_POS_FRAMES, CV_CAP_PROP_POS_AVI_RATIO


原创粉丝点击