FFmpeg muxing到avi

时间:2015-09-02 08:56:26

标签: ffmpeg h.264 avi

我有程序,使用SDL成功显示h264流:我得到h264帧,使用ffmpeg解码并使用SDL绘制。 我也可以将帧写入文件(使用fwrite)并通过ffplay播放此文件。

但我想将数据复制到avi并在av_write_frame中遇到一些问题。

这是我的代码:

...
/*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
}

此外,我尝试使用av​​codec_encode_video2(将pFrame编码回H264)旁边的SDL东西,但编码不起作用 - 我有空包:(这是第二个问题。

使用av_interleaved_write_frame会导致访问冲突。

我从ffmpeg muxing示例(https://www.ffmpeg.org/doxygen/2.1/doc_2examples_2muxing_8c-example.html)复制的复用部分的代码

0 个答案:

没有答案