FFmpeg에서 avcodec_encode_video 압축되지 않는문제

lhwpro의 이미지

안녕하세요.
X-Window에서 동작하는 FFmpeg을 이용한 화면 녹화 프로그램을 만들고 있는데
막히는 부분이 있어 계속 찾아 보다가 혹시나 조언을 구할수 있을까 하여 이렇게 포스팅합니다.

녹화가 시작되면 X-Window 화면 캡쳐 및 Mpeg4 Video로 인코딩 및 파일로 저장하는 루틴이 별도의 스레드를 통해 계속 반복하게 됩니다.

지금 저의 문제는 캡쳐후 비디오 파일로 저장하기 바로전에 avcodec_encode_video를 호출하여 영상을 Encode하는데 전혀 압축되지 않은 결과가 계속 반환 되는 문제입니다.
그래서 몇 초만 저장해도 비디오 파일이 무지 큽니다.
115200만큼의 사이즈를 가지는 이미지를 Encode해도 out_size는 115200으로
반환됩니다.

MPEG4 VIDEO Codec을 생성하여 루틴을 돌리는데
이것참 아무리 건드려 봐도 outSize가 줄어들지를 않네요..
관련 정보 아시는분 조언좀 부탁드리겠습니다. ㅠ.ㅠ

void saveCapturedImage(FILE *fp, XImage *pImage)
{
	AppContext *pApp = getAppContext();
	int outSize, i;
 
	//To validate captured image
	//saveXImageToBitmap(pImage);	
 
	if(pApp->state & DV_START)
	{
		av_register_all();
 
		//Determine AVOutputFormat
		pFileOutputFormat = guess_format("avi",NULL,NULL);
		if(!pFileOutputFormat)
		{
			DavidMessageBox("Error to guess format");
			exit(1);
		}
 
		pOutputFile = av_alloc_format_context();
		if(!pOutputFile)
		{
			DavidMessageBox("Error allocating memory for format context");
			exit(1);
		}
 
		pOutputFile->oformat = pFileOutputFormat;
		if(pOutputFile->oformat->priv_data_size > 0)
		{
			pOutputFile->priv_data = av_mallocz(pOutputFile->oformat->priv_data_size);			
		}
 
		pOutputFile->preload = (int)(0.5 * AV_TIME_BASE);
		pOutputFile->max_delay = (int)(0.7 * AV_TIME_BASE);
 
		//Add the video stream and initilize the codecs
		//Prepare video stream
		pOutputStream = addVideoStream(pOutputFile, pImage, PIX_FMT_RGB24, CODEC_ID_MPEG4);
 
		if(av_set_parameters(pOutputFile, NULL) < 0)
		{
			DavidMessageBox("Invalid encoding parameters");
			exit(0);
		}	
 
		//Open the codec we will use
		if(avcodec_open(pOutputStream->codec, pCodec) < 0)
		{
			fprintf(stderr, "Could not open video codec\n");
			exit(1);
		}
 
		//We should implement the code for audio capturing here
 
		/*
		 * Prepare image buffer and frame that will hold captured data
		 */
		//Input Frame(Picture)		
		pInputFrame = avcodec_alloc_frame();
 
		//We should implement the code to process image which has 8 bit palette following
		avpicture_fill((AVPicture *)pInputFrame, (uint8_t *)pImage->data,
					   PIX_FMT_RGB32, pImage->width, pImage->height);
 
		//Output Frame(Picture)
		pOutputFrame = avcodec_alloc_frame();
 
		outputImageSize = avpicture_get_size(pOutputStream->codec->pix_fmt,
											 pOutputStream->codec->width, pOutputStream->codec->height);
		pOutputPicBuffer = av_malloc(outputImageSize);
		if(!pOutputPicBuffer)
		{
			fprintf(stderr, "Couldn't allocate buffer for output picture buffer\n");
			exit(1);
		}
		avpicture_fill((AVPicture *)pOutputFrame, pOutputPicBuffer, 
						pOutputStream->codec->pix_fmt, pOutputStream->codec->width,
	  					pOutputStream->codec->height);
 
		/*
		 * Prepare output buffer for encoded frames
		 */
		if((outputImageSize + 200) < FF_MIN_BUFFER_SIZE)
		{
			outputEncodedFrameBufferSize = FF_MIN_BUFFER_SIZE;
		}
		else
		{
			outputEncodedFrameBufferSize = outputImageSize + 200;
		}
		pOutputEncodedFrameBuffer = malloc(outputEncodedFrameBufferSize);
		if(!pOutputEncodedFrameBuffer)
		{
			fprintf(stderr, "Couldn't allocate buffer for encoded frame buffer\n");
			exit(1);
		}
 
		//Image resampling
		if(!pImgResampleCtx)
		{
			pImgResampleCtx = sws_getContext(pImage->width, pImage->height, PIX_FMT_RGB32,
											 pOutputStream->codec->width,
											 pOutputStream->codec->height,
											 pOutputStream->codec->pix_fmt, 1, NULL, NULL, NULL);
		}
 
		sprintf(pOutputFile->filename, "temp_movie.mpeg");
 
		if(url_fopen(&pOutputFile->pb, pOutputFile->filename, URL_WRONLY) < 0)
		{
			DavidMessageBox("Couldn't open url_fopen");
		}
 
		if(av_write_header(pOutputFile) < 0)
		{
			DavidMessageBox("Couldn't write header for output file");
		}
	}
 
	/*
	 * Convert input picture to pixel format the encoder expects
	 */
	//Image resampling and conversion
 
	if(sws_scale(pImgResampleCtx, pInputFrame->data, pInputFrame->linesize, 0,
		  		 pImage->height, pOutputFrame->data, pOutputFrame->linesize) < 0)
	{
		fprintf(stderr, "Error converting or resampling frame\n");
		exit(1);
	}
 
	/*
	 * Encode the image to capture
	 */
	outSize = avcodec_encode_video(pOutputStream->codec, pOutputEncodedFrameBuffer, 
									outputEncodedFrameBufferSize, pOutputFrame);
	if(outSize < 0)
	{
		fprintf(stderr, "Error encoding frame: codec %p, encodedbuffer %p\n", pOutputStream->codec,
				pOutputEncodedFrameBuffer);
		exit(1);
	}
 
	/*
	 * Write output frame to file
	 */
	if(outSize > 0)
	{
		writeVideoOut(pOutputFile, pOutputStream, pOutputEncodedFrameBuffer, 
					  outputEncodedFrameBufferSize);
	}
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.