Skip to main content
GameDev.net gamedev.net
🔒 Locked

opengl program - mpeg output

Started by AndreTheGiant Oct 5, 2004 at 6:17 PM 3 replies 1.3k views
Original Post
AndreTheGiant
AndreTheGiant
If I have an opengl application (either interactive game, or just a non-interactive demo), how easy is it to turn the output of the program into a movie file, such as mpeg, avi, or anything else? What are the issues? Do you have to do exactly 30 frames per second, or can you output frames as often/rarely as you want? Has anyone done this? How scary is the code to do so? Do you have any links?
Cibressus
Cibressus
should'nt be that hard, just save each frame you render as a image then use a program with the system() command with the series of images as your arguement.
lonesock
lonesock
I just started playing around with libavcodec for crossplatform mpeg writing.

Here's my super-ugly source.

#include <iostream>#include <stdlib.h>#include <fstream>#include "avcodec.h"using namespace std;int main(int argc, char *argv[]){	//	Test a video encode		AVCodec *codec;	AVCodecContext *c = NULL;	AVFrame *picture;	int CompBufSize, out_size;	unsigned char *CompBuf;	int PicBufSize;	unsigned char *PicBuf;	int i, j, fr;	ofstream fout;	char TheString [1024];		//	Initialize avcodec	avcodec_init ();	//	And register all codecs	avcodec_register_all ();	cout << "avcodec version " << avcodec_version () << endl;	cout << "avcodec build " << avcodec_build () << endl;	if (avcodec_build ()!= LIBAVCODEC_BUILD)	{	    cout << "Mismatched buid!!" << endl;		cout << "Expected build = " << LIBAVCODEC_BUILD << endl;		return (-1);	}				//	stick with MPEG1, or I need to include the avformat library	codec = avcodec_find_encoder (CODEC_ID_MPEG1VIDEO);	//codec = avcodec_find_encoder (CODEC_ID_MJPEG);	//codec = avcodec_find_encoder (CODEC_ID_MSMPEG4V3);	if (!codec)	{	    //	something's wrong	    cout << "Oops, could not find the desired CODEC" << endl;	    system("PAUSE");	    return (-1);	}			//	OK, I have the codec I want, get the context and frame info started	c = avcodec_alloc_context ();	picture = avcodec_alloc_frame ();		//	configure my desired video stream params	c->bit_rate = 100000;	/*	c->bit_rate_tolerance = c->bit_rate;    c->qmin= 1;    c->qmax= 31;    c->max_qdiff = 3;    */    c->me_method= ME_EPZS;	c->gop_size = 15;	//c->max_b_frames = 1;	c->width = 352;	c->height = 240;	c->pix_fmt = PIX_FMT_RGB24;	c->frame_rate = 2997;	c->frame_rate_base = 100;		//	and open it	if (avcodec_open (c, codec) < 0)	{	    //	more bad	    cout << "Bad boy, could not open " << codec->name << endl;	    system("PAUSE");	    return (-1);	}    		cout << "Opened " << codec->name << endl;	system("PAUSE");		//	OK, ready to begin	fout.open ("test.mpg", ios::out | ios::binary);		//	get the buffer RAM	CompBufSize = 1000000;	CompBuf = new unsigned char [CompBufSize];	PicBufSize = (c->width * c->width);	PicBuf = new unsigned char [PicBufSize * 3];		//	Set up the frame	picture->data[0] = PicBuf;	picture->data[1] = picture->data[0] + PicBufSize;	picture->data[2] = picture->data[1] + PicBufSize;	picture->linesize[0] = c->width;	picture->linesize[1] = c->width;	picture->linesize[2] = c->width;	//picture->quality = 30;		//	save some pictures	for (fr = 0; fr < 300; ++fr)	{	    //	this frame	    for (j = 0; j < PicBufSize * 3; ++j)	    	PicBuf [j] = (3 * j + 7 * fr) & 255;	    			//	and encode it	    out_size = avcodec_encode_video (c, CompBuf, CompBufSize, picture);	    fout.write ((char*)CompBuf, out_size);	    //cout << "frame size = " << out_size << endl;	}    		//	and latent data?	while (out_size)	{	    out_size = avcodec_encode_video (c, CompBuf, CompBufSize, NULL);	    fout.write ((char*)CompBuf, out_size);	    //cout << "finishing size = " << out_size << " ";	}   		//fout.write ("\0x00\0x00\0x01\0xb7", 4);		avcodec_string (TheString, 1023, c, true); 	cout << TheString << endl;		//	clean up	fout.close ();	avcodec_close (c);	free (c);	free (picture);	  system("PAUSE");	  return 0;}


It's really easy, except it's using YUV, not RGB (I'm not sure I'm doing that right). They have a function called "img_convert", but it takes AVPictures, not AVFrames...so i need a bit of wrapping. The documentation is way behind...but their example source code is pretty nice.
Cibressus
Cibressus
Quote:
Original post by AndreTheGiant
A program with the system() command?


if you don't know about the system 'command' (it's called a function) then i suggest you go learn some more C++ before going onto such a majro project.

edit: crap, i just realised i used command instead of function in my orignal post. boy i feel hypocritical now.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.