Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm working on a FFMPEg Project and trying to write a file with a stream. But when I call avformat_write_header it always throws an errr. Here's the code:

C++
AVCodecContext *codecctx;
	AVFormatContext *fmtctx;
	fmt=av_guess_format(NULL,filename,NULL);
	if(!fmt)
		printf("Das Format kann nicht erkannt werden. MPEG wird verwendet");
	AVCodec *codec = avcodec_find_encoder(fmt->video_codec);
	codecctx = avcodec_alloc_context3(codec);
	codecctx->codec_id = fmt->video_codec;
	codecctx->codec_type = AVMEDIA_TYPE_VIDEO;
	codecctx->gop_size = 12;
	codecctx->bit_rate = WIDTH*HEIGHT*4;
	codecctx->width = WIDTH;
	codecctx->height = HEIGHT;
	codecctx->time_base.den = 2; // Framerate kann man nachträglich verändern. 0.2 hier bedeutet dass jedes Bild für 5 Sekunden erscheint. 
	codecctx->time_base.num =1;
	//codecctx->time_base =(AVRational){1,FPS};
	codecctx->max_b_frames=1;
	codecctx->pix_fmt= STREAM_PIX_FMT;
	fmtctx = avformat_alloc_context();
    fmtctx->oformat = fmt;
    fmtctx->video_codec_id = fmt->video_codec;
	avcodec_open2(codecctx, codec, NULL);
	if(avcodec_open2(codecctx, codec, NULL)<0)
		printf("Codec nicht geoeffnet");
	AVStream *VideoStream = avformat_new_stream(fmtctx,0);
	VideoStream->codecpar->codec_id=fmt->video_codec;
	VideoStream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
	VideoStream->codecpar->format = STREAM_PIX_FMT;
	VideoStream->codecpar->width=WIDTH;
	VideoStream->codecpar->height=HEIGHT;
	VideoStream->time_base.den=0.2;
	VideoStream->time_base.num=1;
	
	avformat_write_header(fmtctx,0);

And here's the error:
Unhandled exception at 0x0070848D (avformat-57.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x0000000C.

What I have tried:

I've try to remove the write Header line and it runs well.
Posted
Updated 9-Jun-16 2:40am
v2
Comments
Richard MacCutchan 9-Jun-16 5:05am    
I do not know this library but can only assume that you have missed something in the fmtctx block that has led to the access violation. You should check the documentation to ensure that all mandatory fields are being initialised.
KarstenK 9-Jun-16 5:38am    
it is obvious that your fmtctx isnt somehow properly initialized. But dont know hot to fix it.

Like Richard I do not know ffmpeg well. But the documentation for avformat_write_header[^] states for the first parameter:
Quote:
Media file handle, must be allocated with avformat_alloc_context(). Its oformat field must be set to the desired output format; Its pb field must be set to an already opened AVIOContext.

You do not set the pb field in your code.

BTW: Why you are calling avcodec_open2 twice?
 
Share this answer
 
The Problem is i forgot to call avio_open. Now I'm trying to add a png Image to the stream, but don't know how to convert it to an useable Frame. Can you give me some hints?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900