我正在使用FFMPEg进行项目,但是以某种方式我无法创建任何媒体文件。我已经设置了参数,编解码器等,但是当我调用avformat_write_header时,该应用程序将停止工作。
这是我的代码:

AVOutputFormat *fmt;
AVCodecContext *codecctx;
AVFormatContext *fmtctx;
fmt=av_guess_format(NULL,filename,NULL);
    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;
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);
    AVStream *VideoStream = avformat_new_stream(fmtctx,codec);
avformat_write_header(fmtctx,NULL);

最佳答案

用于混合的主要API函数是用于写入文件头的avformat_write_header(),用于写入数据包的av_write_frame()/ av_interleaved_write_frame()和用于完成文件的av_write_trailer()。

检查此链接

http://ffmpeg.org/doxygen/trunk/group__lavf__encoding.html#details

07-28 04:41