FFMPEG写头错误 [英] FFMPEG write header error

查看:476
本文介绍了FFMPEG写头错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究FFMPEg项目并试图用流编写一个文件。但是当我调用avformat_write_header时,它总是抛出一个错误。这是代码:



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:

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);



这是错误:

ConsoleApplication1.exe中0x0070848D(avformat-57.dll)的未处理异常:0xC0000005:访问冲突读取位置0x0000000C。



我尝试了什么:



我试图删除写标题行并运行良好。


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.

推荐答案

像理查德一样,我不太了解ffmpeg。但 avformat_write_header 的文档[ ^ ]表示第一个参数:

Like Richard I do not know ffmpeg well. But the documentation for avformat_write_header[^] states for the first parameter:
引用:

媒体文件句柄,必须使用avformat_alloc_context()进行分配。其oformat字段必须设置为所需的输出格式;其pb字段必须设置为已打开的AVIOContext。

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.



您没有在代码中设置 pb 字段。



BTW:你为什么两次打电话给 avcodec_open2


问题是我忘了打电话给avio_open。现在我正在尝试将png图像添加到流中,但不知道如何将其转换为可用的帧。你能给我一些提示吗?
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?


这篇关于FFMPEG写头错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆