Ask Your Question
2

Cutting selected frame ranges from a video

asked 2013-01-28 06:55:32 -0600

Bart Vandewoestyne gravatar image

I am quite new to OpenCV and would like to know what is the best approach to cut selected ranges of frames from a video-file. Say for example you have a video-file VideoIn.avi with 10000 frames, and you want to extract frames 100->500 into output video VideoOut1.avi, and frames 7000->8000 into output video VideoOut2.avi. What would be the best approach using OpenCV?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
3

answered 2013-01-28 07:45:08 -0600

Haris gravatar image

updated 2013-01-28 23:07:11 -0600

Hi you can try the below code.

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <stdio.h>
#include <iostream>

using namespace std;


int main(int argc, char *argv[])
 {
  int i=0;
  int frame_no=0;
  char name[1000];
  cvNamedWindow("mainWin");
  CvVideoWriter *writer = 0;
  bool recording = false;

CvCapture* capture = cvCreateFileCapture( "video.avi"); 
if (argv[1]==NULL)
{
    cout<<"Cannot laod video"<<endl;
    return 0;
}

IplImage* frame;
cout<<"Press 's' to start recording, 'q' to stop recording 'Esc' to exit"<<endl;

while (1)
{
    cout<<"frame number"<<frame_no++<<endl;
    frame = cvQueryFrame( capture );
    if ( !frame ) break;
    cvShowImage( "mainWin", frame );
    char c = cvWaitKey(33);

    if ((c=='s')&&(recording==false))
    {
        recording=true;
        sprintf(name,"%d.avi",i);
        writer=cvCreateVideoWriter(name,CV_FOURCC('M', 'P', '4', '2'),15,cvSize(640,480),1);
        i++;
    }

    if (recording==true)
    {
        cvWriteFrame(writer,frame);      // add the frame to the file
        cvShowImage( "Output", frame );
    }

    if ((c=='q')&&(recording==true))
    {
        recording=false;
        cvReleaseVideoWriter(&writer);
    }

    if ( c == 27 ) break;
}

cvReleaseCapture( &capture );
if (writer!=NULL) cvReleaseVideoWriter(&writer);
return 0;
}

Hope these helpful...

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-01-28 06:55:32 -0600

Seen: 8,322 times

Last updated: Jan 28 '13