Adding progress bar on Videos

To create a slider, we call cvCreateTrackbar() and indicate which window we would like the trackbar to appear in. The cvCreateTrackbar() has the following syntax.

int cvCreateTrackbar(const char* trackbarName, const char* windowName, int* value, int count, CvTrackbarCallback onChange);
trackbarName  Name of the created trackbar.
windowName  Name of the window which will be used as a parent for created trackbar.
value Pointer to an integer variable, whose value will reflect the position of the slider. Upon creation, the slider position is defined by this variable.
count Maximal position of the slider. Minimal position is always 0.
onChange Pointer to the function to be called every time the slider changes position. This function should be prototyped as void Foo(int); Can be NULL if callback is not required.

The function cvCreateTrackbar creates a trackbar with the specified name and range, assigns a variable to be syncronized with trackbar position and specifies a callback function to be called on trackbar position change. The created trackbar is displayed on the top of the given window.

Program

#include "cv.h"
#include "highgui.h"
int g_slider_position = 0;
CvCapture* capture = NULL;
void onTrackbarSlide(int pos)
{
	cvSetCaptureProperty(capture, CV_CAP_PROP_POS_FRAMES, pos);
}
int main(int argc, char** argv)
{
	cvNamedWindow("progressBarOnVideo", CV_WINDOW_AUTOSIZE);
	capture = cvCreateFileCapture("Megamind.avi");
	int frames = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
	if (frames != 0)
	{
		cvCreateTrackbar("Position", "progressBarOnVideo", &g_slider_position,
				frames, onTrackbarSlide);
	}
	IplImage* frame;

	while (1)
	{
		frame = cvQueryFrame(capture);
		if (!frame)
			break;
		cvShowImage("progressBarOnVideo", frame);
		char c = cvWaitKey(33);
		if (c == 27)
			break;
	}
	cvReleaseCapture(&capture);
	cvDestroyWindow("progressBarOnVideo");
	getchar();
	return (0);
}