Opencv reads MP4, MPEG, WMV, FLV, mov, 3GP etc. on Win32

Source: Internet
Author: User

Source: http://wsyjwps1983.blog.163.com/blog/static/6800900120123181500785/
Fily logs-Netease blog

13:50:00 | classification: image and video codec | font size subscription

14:41:56 | classification: Pattern Recognition | Tag:

1. method 1

Cfiledialog DLG (true, _ T ("*. MP4 "), null, ofn_filemustexist | ofn_pathmustexist | ofn_hidereadonly, _ T (" video files (*. avi) | *. avi | all files (*. *) | *. * | "), null );

Cstring path;

DLG. m_ofn.lpstrtitle = _ T ("openvedio ");

If (DLG. domodal () = idok ){

Path = DLG. getpathname ();
} Else
Return;
Iplimage * m_pframe = NULL; // declare IPL/opencv image pointer

Hresult hr;
// Define the imediadet interface instance
Ccomptr <imediadet> pdet;

HR = pdet. cocreateinstance (_ uuidof (mediadet ));
If (failed (HR ))
Return;

// Convert the video file name to the BSTR type
Ccombstr openbstr (PATH );
// Set the file association for the imediadet Interface
HR = pdet-> put_filename (openbstr );
If (failed (HR ))
Return;

// Retrieve video streams and audio streams from a video
Long lstreams;
HR = pdet-> get_outputstreams (& lstreams );
If (failed (HR ))
Return;

// Extract the video stream of the video because the frame information is stored in the video stream.
Bool bfound = false;
For (INT I = 0; I <lstreams; I ++)
{
Guid major_type;
HR = pdet-> put_currentstream (I );
If (succeeded (HR ))
HR = pdet-> get_streamtype (& major_type );
If (failed (HR ))
Break;
If (major_type = mediatype_video)
{
Bfound = true;
Break;
}
}
If (! Bfound)
Return;

Long width = 0, Height = 0; // store the width and height of the bitmap (unit: pixels)
Am_media_type MT;
HR = pdet-> get_streammediatype (& mt );
If (succeeded (HR ))
{
If (mt. formattype = format_videoinfo) & (mt. cbformat> = sizeof (videoinfoheader )))
{
// Get the videoinfoheader structure pointer. The videoinfoheader structure includes some
// Related information, which contains the bitmapinforheader Structure
Videoinfoheader * pvih = (videoinfoheader *) (mt. pbformat );
Width = pvih-> bmiheader. biwidth;
Height = pvih-> bmiheader. biheight;
If (height <0) height * =-1;
}
Else
HR = vfw_e_invalidmediatype;
If (mt. cbformat! = 0)
{
Cotaskmemfree (pvoid) Mt. pbformat );
Mt. cbformat = 0;
}

If (mt. Punk! = NULL)
{
Mt. Punk-> release ();
Mt. Punk = NULL;
}

}
If (failed (HR ))
Return;

Long size;
Double time = 0.0;
Double totaltime;

Double rate;

// Obtain the length of the video.
Pdet-> get_streamlength (& totaltime );
// The number of frames per second
Pdet-> get_framerate (& rate );

Cvnamedwindow ("result", 1 );

// Totaltime = totaltime * rate;
M_pframe = cvcreateimage (cvsize (width, height), ipl_depth_8u, 3 );
// Read one frame per second
For (time = 0.0; time <totaltime; Time + = 1.0)
{
/*
HR = pdet-> enterbitmapgrabmode (time );
If (failed (HR ))
{
Printf ("failed in enterbitmapgrabmode! HR = 0x % x \ r \ n ", HR );
Return;
}

Ccomptr <isamplegrabber> pgrabber = NULL;
HR = pdet-> getsamplegrabber (& pgrabber );

Pgrabber-> getcurrentbuffer (& size, null );
Long m_nbuffersize = size;
Pgrabber-> getcurrentbuffer (& m_nbuffersize, (long *) m_pframe-> imagedata );
*/
///*
HR = pdet-> getbitmapbits (time, & size, 0, width, height );

If (failed (HR ))
Return;

Char * pbuffer = new char [size];
If (! Pbuffer)
Return;
HR = pdet-> getbitmapbits (time, 0, pbuffer, width, height );
If (failed (HR ))
Return;

M_pframe-> imagedata = pbuffer + sizeof (bitmapinfoheader );
//*/
Cvflip (m_pframe );
// Detect_and_draw (m_pframe );
Cvshowimage ("result", m_pframe );
Cvwaitkey (10 );
Delete [] pbuffer;
// Afxmessagebox ("T ");

}
Cvreleaseimage (& m_pframe );
Cvdestroywindow ("result ");

2 method: # include "stdafx. H"
# Include "cv. H"
# Include "highgui. H"
# Include "ffopencv. H"

Int main (INT argc, char * argv [])
{

Iplimage * frame = NULL;

Cvcapture * capture;
Capture = cvcapturefromfile_ffmpeg ("Lake. mpg"); // D: \ downloads \ guns and roses \ Guns N' Roses-welcome to the jungle. mpg ");
// Capture = cvcreatefilecapture ("Lake. mpg ");
// Cvsize imgsize;

Cvnamedwindow ("videos", cv_window_autosize );
// Cvresizewindow ("video1", 110,1000 );
Cvmovewindow ("videos", 10, 50); // locate the original image window

Frame = cvqueryframe (capture );
Int numtotal = 100000;
For (INT num = 1; num <numtotal; num ++)
{

Frame = cvqueryframe (capture );
Cvshowimage ("videos", frame );
Cvwaitkey (1, 100 );
}

Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.