⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmwithdirectshowmultivideo.cpp

📁 VideoMan is a very easy image acquisition library. It is able to manage many video inputs at the sam
💻 CPP
字号:
#include <windows.h>
#include <GL/glut.h>
#include <iostream>

#include "VideoManControl.h"
#include "VideoManInputFormat.h"

using namespace std;

/*
This is an example using VideoMan with DirectShow and OpenGL.
Multiple video files are initilized using DirectShow
To use this example, VideoMan must be built with the directive VM_OGLRenderer, 
also you need to build the input VMDirectShow
*/

VideoManControl videoMan;
int screenLeft, screenUp, screenWidth, screenHeight;
bool fullScreened;
double videoLength;
bool activated; //To know if the first input is activated or not
int visualMode = 0;
int mainInput = 0;
std::vector< int > videoInputIDs; //List of indexes of the initialized inputs

size_t maxVideos = 10;
bool renderAudio = true;
string dirPath;


void glutResize(int width, int height)
{
	screenLeft = 0;
	screenUp = 0;
	screenWidth = width;
	screenHeight = height;
	//Notify to VideoMan the change of the screen size
	videoMan.changeScreenSize( screenLeft, screenUp, screenWidth, screenHeight );
}


void glutKeyboard(unsigned char key, int x, int y)
{
	switch (key)
	{
		case 27:
		{
			exit(0);
		}
	}
}


void glutSpecialKeyboard(int value, int x, int y)
{
	switch (value)
    {
		case GLUT_KEY_F1:
		{
			if ( !fullScreened )
				glutFullScreen();
			else
			{
				glutPositionWindow( 0, 20 );
				glutReshapeWindow( 640, 480 );
			}
			fullScreened = !fullScreened;
			break;
		}
		case GLUT_KEY_F2:
		{
			visualMode = (visualMode + 1 ) %2;
			videoMan.changeVisualizationMode( visualMode);
			break;
		}		
		case GLUT_KEY_F3:
		{
			mainInput = ( mainInput + 1 ) %videoInputIDs.size();
			videoMan.changeMainVisualizationInput( mainInput );
			break;
		}
    }
}


void InitializeOpenGL()
{

}

void loadFiles( string dirPath, vector< string > &files )
{
	WIN32_FIND_DATA fd;
	DWORD dwAttr = FILE_ATTRIBUTE_ARCHIVE;
	const std::string path  = dirPath + "/*.*";
	HANDLE hFind = FindFirstFile( path.c_str(), &fd);
	if(hFind == INVALID_HANDLE_VALUE)
	{
		FindClose( hFind);
		return;
	}
	do
	{
		if ( fd.dwFileAttributes & dwAttr )
		{
			std::string fileName = dirPath + "/" + fd.cFileName;			 
			/*if ( fileName.rfind(".dll") != std::string::npos )
			{
			}*/
			files.push_back( fileName );

		}
	}while( FindNextFile( hFind, &fd));
	FindClose( hFind);
}

bool InitializeVideoMan()
{
	vector< string > files;
	loadFiles( dirPath, files );

	VideoManInputFormat format;	
	inputIdentification device;

	for ( size_t v = 0; v < files.size() && videoInputIDs.size() < maxVideos; ++v )
	{
		//Initialize one input from a video file
		device.fileName = files[v]; //file name
		device.identifier = "DSHOW_VIDEO_FILE"; //using directshow	
		format.timeFormat = SECONDS; //We want the time format in seconds
		//play in real-time
		format.clock = true;
		format.dropFrames = true;
		format.renderAudio = renderAudio;
		int inputID;
		if ( ( inputID = videoMan.addVideoInput( device, &format ) ) != -1 )
		{
			cout << endl;
			cout << "Loaded video file: " << device.fileName << endl;
			cout << "resolution: " << format.width <<" " << format.height << endl;
			//get the length of the video
			videoLength = videoMan.getLength( inputID );
			printf("duration: %f seconds\n\n", videoLength );
			videoInputIDs.push_back( inputID );
		}	
	}
	for ( size_t v = 0; v < videoInputIDs.size(); ++v )
		videoMan.playVideo( v );
	
	//We want to display all the intialized video inputs
	videoMan.activateAllVideoInputs();	
	activated = true;

	return ( videoInputIDs.size() > 0);
}


void glutDisplay(void)
{
	//Clear the opengl window
	glClear( GL_COLOR_BUFFER_BIT );
	//For each initialized inputs
	for ( size_t n=0; n < videoInputIDs.size(); n++ )
	{
		//Get a new frame from input n
		char *image = videoMan.getFrame( n );
		if ( image != NULL )
		{
			//Update the texture of the renderer
			videoMan.updateTexture( n ); 
            
			/*
				Process the image...
			*/

			//Release the frame
			videoMan.releaseFrame( n );
		}
		//render the image of input n in the screen
		videoMan.renderFrame( n ); 
	}

	//Check if the video file (input number 0) has reached the end	
	if ( videoMan.getPosition(0) == videoLength )
		videoMan.goToFrame( 0, 0 ); //restart from the begining

	glFlush();
    glutSwapBuffers();
}


void showHelp()
{
	cout << "========" << endl;
	cout << "keys:" << endl;
	cout << "Esc->Exit" << endl;
	cout << "F1->Fullscreen" << endl;
	cout << "F2->Switch Visualization Mode" << endl;
	cout << "F3->Switch Main Input" << endl;
	cout << "========" << endl;
}

int main(int argc, char** argv)
{
	cout << "Multiple video files are initilized using DirectShow" << endl;
	cout << "Usage: VMwithDirectShowMultiVideo.exe directoryPath(string) playAudio(0/1)" << endl;
	cout << "Example: VMwithDirectShowMultiVideo.exe c:\\MyVideos 0" << endl;
	cout << "=====================================================" << endl;
	if ( argc > 1 )
		dirPath = argv[1];
	if ( argc > 2 )
	{
		string num = argv[2];
		renderAudio = ( num != "0" );
	}
	
	glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA | GLUT_MULTISAMPLE );
    glutInitWindowPosition( 0, 0 );
    glutInitWindowSize( 640, 480 );
    glutInit( &argc, argv );

    glutCreateWindow("VideoMan with DirectShow");

    glutReshapeFunc(glutResize);
    glutDisplayFunc(glutDisplay);
    glutIdleFunc(glutDisplay);
    glutKeyboardFunc(glutKeyboard);
	glutSpecialFunc(glutSpecialKeyboard);

    InitializeOpenGL();
	
	if ( !InitializeVideoMan() )
	{
		return 0;
	}
	
	fullScreened = false;

	showHelp();

    glutMainLoop();

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -