FFmpeg多路复用到avi

FFmpeg多路复用到avi,ffmpeg,h.264,avi,Ffmpeg,H.264,Avi,我有一个程序,成功地用SDL显示h264流:我得到h264帧,用ffmpeg解码,用SDL画图。 我还可以将帧写入文件(使用fwrite)并通过ffplay播放该文件 但是我想将数据多路复用到avi,并且在AVU写入帧中面临一些问题 这是我的密码: ... /*Initializing format context - outFormatContext is the member of my class*/ AVOutputFormat *outFormat; outFormat = av_gu

我有一个程序,成功地用SDL显示h264流:我得到h264帧,用ffmpeg解码,用SDL画图。 我还可以将帧写入文件(使用fwrite)并通过ffplay播放该文件

但是我想将数据多路复用到avi,并且在AVU写入帧中面临一些问题

这是我的密码:

...
/*Initializing format context - outFormatContext is the member of my class*/
AVOutputFormat *outFormat;
outFormat = av_guess_format(NULL,"out.avi",NULL);
outFormat->video_codec = AV_CODEC_ID_H264;
outFormat->audio_codec = AV_CODEC_ID_NONE;
avformat_alloc_output_context2(&outFormatContext, outFormat, NULL, "out.avi");
AVCodec *outCodec;
AVStream *outStream = add_stream(outFormatContext, &outCodec, outFormatContext->oformat->video_codec);
avcodec_open2(outStream->codec, outCodec, NULL);
av_dump_format(outFormatContext, 0, "out.avi", 1);
if (avio_open(&outFormatContext->pb, "out.avi", AVIO_FLAG_WRITE) < 0) 
    throw Exception("Couldn't open file");
if (avformat_write_header(outFormatContext, NULL) < 0)
    throw Exception("Couldn't write to file");
 //I don't have exceptions here - so there is 6KB header in out.avi.
 ...

static AVStream *add_stream(AVFormatContext *oc, AVCodec **codec,
                        enum AVCodecID codec_id)
{
AVCodecContext *c;
AVStream *st;
/* find the encoder */
*codec = avcodec_find_encoder(codec_id);
if (!(*codec)) 
    throw("Could not find encoder");
st = avformat_new_stream(oc, *codec);
if (!st) 
    throw ("Could not allocate stream");
st->id = oc->nb_streams-1;
c = st->codec;
c->bit_rate = 400000;
/* Resolution must be a multiple of two. */
c->width    = 1920;
c->height   = 1080;
c->pix_fmt  = PIX_FMT_YUV420P;
c->flags = 0;
c->time_base.num = 1;
c->time_base.den = 25;
c->gop_size      = 12; /* emit one intra frame every twelve frames at most */
return st;
}
...
/* Part of decoding loop. There is AVPacket packet - h264 packet;
int ret = av_write_frame(outFormatContext, &packet); //it return -22 code - Invadlid argument;
if (avcodec_decode_video2(pCodecCtx, pFrame, &frameDecoded, &packet) < 0) 
    return;
if (frameDecoded)
{
   //SDL stuff
}
。。。
/*初始化格式上下文-outFormatContext是我的类的成员*/
AVOutputFormat*输出格式;
outFormat=av_guess_格式(NULL,“out.avi”,NULL);
outFormat->video\u codec=AV\u codec\u ID\u H264;
outFormat->audio\u codec=AV\u codec\u ID\u NONE;
avformat_alloc_output_context2(&outFormatContext,outFormat,NULL,“out.avi”);
AVCodec*outCodec;
AVStream*outStream=add_流(outFormatContext,&outCodec,outFormatContext->oformat->video_codec);
avcodec_open2(超流->编解码器,超编解码器,空);
av_转储_格式(outFormatContext,0,“out.avi”,1);
如果(avio_打开(&outFormatContext->pb,“out.avi”,avio_标志_写入)<0)
抛出异常(“无法打开文件”);
if(avformat\u write\u头(outFormatContext,NULL)<0)
抛出异常(“无法写入文件”);
//我这里没有例外-因此out.avi中有6KB的头。
...
静态AVStream*添加_流(AVFormatContext*oc,AVCodec**codec,
枚举AVCodecID(编解码器id)
{
avc*c;
AVStream*st;
/*找到编码器*/
*codec=avcodec\u find\u编码器(codec\u id);
如果(!(*编解码器))
抛出(“找不到编码器”);
st=avformat\ U new\ U流(oc,*编解码器);
如果(!st)
抛出(“无法分配流”);
st->id=oc->nb\U-1;
c=st->codec;
c->比特率=400000;
/*分辨率必须是2的倍数*/
c->宽度=1920;
c->高度=1080;
c->pix_fmt=pix_fmt_YUV420P;
c->flags=0;
c->time_base.num=1;
c->time_base.den=25;
c->gop_size=12;/*最多每12帧发射一帧内帧*/
返回st;
}
...
/*解码循环的一部分。有AVPacket-h264包;
int-ret=av_写入_帧(outFormatContext和数据包)//它返回-22 code-Invadlid参数;
if(avcodec_decode_video2(PCODECTX、pFrame、帧解码和数据包)<0)
返回;
如果(帧解码)
{
//SDL材料
}
此外,我还尝试在SDL文件旁边使用avcodec_encode_video2(将pFrame编码回H264),但编码不起作用-我得到了空数据包:(这是第二个问题)

使用av_交错_write_帧会导致访问冲突

复制自ffmpeg muxing示例()的muxing第一部分代码