1.10.3 Importing Videos

Version Info

Minimum Origin Version Required: Origin 93 SR0

Origin C provides the VideoReader class for reading a video file and importing frame(s) of video to matrix object(s).

To use the VideoReader class, the header file "VideoReader.h" needs to be included in your source code.

#include <..\OriginLab\VideoReader.h>

With the VideoReader class you can open a video file and get the video's properties, such as frame count, frame rate (frame per second), current position, etc. It also provides methods for seeking frame, seeking time, and reading frame(s) into matrix object(s).

The following example will create a new matrix book, seek 10 frames into a video then load 100 frames into 100 matrix objects in the active matrix sheet by skipping every other frame.

#include <..\Originlab\VideoReader.h>  // Include the header file
void Import_Video_Ex1(string strFile = "d:\test.avi") {
	MatrixLayer ml; 
	ml.Create("Origin");  // Create a matrix sheet for the frames
	char str[MAXLINE];
	VideoReader vr;  // Declare a VideoReader
	strcpy(str, strFile);
	if(!vr.Open(str)) {  // Open the video file
		out_str("Failed to open video file!");
		return;
	}
	// Get number of frames
	int iFrameCount = (int)vr.GetFrameCount();
	printf("%u frames\n",iFrameCount);
	// Starting frame
	int iOffset = 10;
	// Specify total frames to read
	int iTotalFrames = 100;
	// Specify frames to skip between each read
	int iSkip = 1; // Read every other frame
	bool bRet = vr.SeekFrame(iOffset);
	vr.ReadFrames(ml, iTotalFrames, iSkip);  // Read frames
	if(vr.ReaderReady()) {  
		vr.Close();  // Close the video reader
	}
}

In this example, time is used as the metric by which we seek and import with time skips..

#include <..\Originlab\VideoReader.h>  // Include the header file
void Import_Video_Ex2(string strFile = "d:\test.avi") {
	MatrixLayer ml; 
	ml.Create("Origin");  // Create a matrix sheet for the frames
	char str[MAXLINE];
	VideoReader vr;  // Declare a VideoReader
	strcpy(str, strFile);
	if(!vr.Open(str)) {  // Open the video file
		out_str("Failed to open video file!");
		return;
	}
	// Get number of frames
	int iFrameCount = (int)vr.GetFrameCount();
	// Get frame rate
	double dFPS = vr.GetFPS();
	double dRunningTime = iFrameCount / dFPS;
	printf("%u frames at %f fps with a running time of %f seconds\n",
		iFrameCount, dFPS, dRunningTime);

	// Setup for read
	double dStartTime = 5; // Begin reading at 5 seconds
	double dSkipLength = 3.333; // Skip 3.333 seconds between reads
	vr.SeekFrame((int) dStartTime * dFPS); // Calculate frame start
	int iSkip = (int) dSkipLength * dFPS; // Calculate frames to skip
	// Calculate number of frames to actually read
	int iTotalFrames = (int) ( (dRunningTime - dStartTime) * dFPS)
	/ (iSkip + 1);
	vr.ReadFrames(ml, iTotalFrames, iSkip);  // Read frames
	if(vr.ReaderReady()) {  
		vr.Close();  // Close the video reader
	}
}