2012-02-07 89 views
2

我正在使用最近的Windows(2011年1月)ffmpeg build并试图在H264中录制视频。这是使用以下设置在MPEG4录制罚款:ffmpeg libx264 AVCodecContext设置

c->codec_id = CODEC_ID_MPEG4; 
c->codec_type = AVMEDIA_TYPE_VIDEO; 
c->width = VIDEO_WIDTH; 
c->height = VIDEO_HEIGHT; 
c->bit_rate = c->width * c->height * 4; 
c->time_base.den = FRAME_RATE; 
c->time_base.num = 1; 
c->gop_size = 12; 
c->pix_fmt = PIX_FMT_YUV420P; 

简单地改变CODEC ID来H264引起avcodec_open()失败(-1)。我找到了一个可能的设置列表How to encode h.264 with libavcodec/x264?。我试过这些,没有设置pix_fmt,avcodec_open()仍然失败,但如果我另外设置c-> pix_fmt = PIX_FMT_YUV420P;然后我得到一个零除异常。

然后我发现这里有几个帖子说我不应该设置任何内容(除了code_id,codec_type,width,height以及可能的bit_rate和pix_fmt),因为库现在自己选择最佳设置。我尝试过各种组合,但仍然avcode_open()失败。

有没有人有什么建议或目前的一些设置?

谢谢。

这里有一组H264设置,这给我描述的问题:

static AVStream* AddVideoStream(AVFormatContext *pOutputFmtCtx, 
int frameWidth, int frameHeight, int fps) 
{ 
AVCodecContext* ctx; 
AVStream* stream; 

stream = av_new_stream(pOutputFmtCtx, 0); 
if (!stream) 
{ 
    return NULL; 
} 

ctx = stream->codec; 

ctx->codec_id = pOutputFmtCtx->oformat->video_codec; //CODEC_ID_H264 
ctx->codec_type = AVMEDIA_TYPE_VIDEO; 
ctx->width = frameWidth;    //704 
ctx->height = frameHeight;   //576 
ctx->bit_rate = frameWidth * frameHeight * 4; 

ctx->coder_type = 1; // coder = 1 
ctx->flags|=CODEC_FLAG_LOOP_FILTER; // flags=+loop 
ctx->me_cmp|= 1; // cmp=+chroma, where CHROMA = 1 
ctx->partitions|=X264_PART_I8X8+X264_PART_I4X4+X264_PART_P8X8+X264_PART_B8X8; // partitions=+parti8x8+parti4x4+partp8x8+partb8x8 
ctx->me_method=ME_HEX; // me_method=hex 
ctx->me_subpel_quality = 7; // subq=7 
ctx->me_range = 16; // me_range=16 
ctx->gop_size = 250; // g=250 
ctx->keyint_min = 25; // keyint_min=25 
ctx->scenechange_threshold = 40; // sc_threshold=40 
ctx->i_quant_factor = 0.71; // i_qfactor=0.71 
ctx->b_frame_strategy = 1; // b_strategy=1 
ctx->qcompress = 0.6; // qcomp=0.6 
ctx->qmin = 10; // qmin=10 
ctx->qmax = 51; // qmax=51 
ctx->max_qdiff = 4; // qdiff=4 
ctx->max_b_frames = 3; // bf=3 
ctx->refs = 3; // refs=3 
ctx->directpred = 1; // directpred=1 
ctx->trellis = 1; // trellis=1 
     ctx->flags2|=CODEC_FLAG2_BPYRAMID+CODEC_FLAG2_MIXED_REFS+CODEC_FLAG2_WPRED+CODEC_FLAG2_8X8DCT+CODEC_FLAG2_FASTPSKIP; // flags2=+bpyramid+mixed_refs+wpred+dct8x8+fastpskip 
ctx->weighted_p_pred = 2; // wpredp=2 
// libx264-main.ffpreset preset 
ctx->flags2|=CODEC_FLAG2_8X8DCT; 
ctx->flags2^=CODEC_FLAG2_8X8DCT; // flags2=-dct8x8 

// if set this get divide by 0 error on avcodec_open() 
// if don't set it get -1 error on avcodec_open() 
//ctx->pix_fmt = PIX_FMT_YUV420P; 

return stream; 

}

+0

您确定h.264编解码器是否包含在ffmpeg版本中? – ciphor 2012-02-07 13:52:04

+0

我正在使用http://ffmpeg.zeranoe.com/builds/ builds。他们说它是用--enable-libx264构建的。 – integra753 2012-02-07 15:57:39

+0

请发布您的代码CODEC_ID_H264 – ciphor 2012-02-08 02:13:23

回答

1

以我的经验,你应该初始化您的编解码器时尽量给予FFMPEG的信息最少。这看起来可能与直觉相反,但这意味着FFMPEG将使用它的默认设置,比您自己的猜测更有可能工作。看看我下面包括:

AVStream *stream; 
m_video_codec = avcodec_find_encoder(AV_CODEC_ID_H264); 
stream = avformat_new_stream(_outputCodec, m_video_codec); 
ctx = stream->codec; 
ctx->codec_id = m_fmt->video_codec; 
ctx->bit_rate = m_AVIMOV_BPS;   //Bits Per Second 
ctx->width = m_AVIMOV_WIDTH;   //Note Resolution must be a multiple of 2!! 
ctx->height = m_AVIMOV_HEIGHT;  //Note Resolution must be a multiple of 2!! 
ctx->time_base.den = m_AVIMOV_FPS;  //Frames per second 
ctx->time_base.num = 1; 
ctx->gop_size  = m_AVIMOV_GOB;  // Intra frames per x P frames 
ctx->pix_fmt  = AV_PIX_FMT_YUV420P;//Do not change this, H264 needs YUV format not RGB 

正如在以前的答案,这里是FFMPEG库编码的工作示例RGB帧的H264视频:

http://www.imc-store.com.au/Articles.asp?ID=276

一个额外的想法在你的代码虽然:

  • 你有没有像下面注册过的所有类似?

    avcodec_register_all();

    av_register_all();

    如果您在代码的开头附近没有调用这两个函数,那么后续对FFMPEG的调用将会失败,您很可能会发生seg-fault。

看看链接的例子,我在VC++ 2010上测试了它,它完美地工作。

相关问题