How to encode h.264 with libavcodec/x264?

szatmary picture szatmary · Aug 24, 2010 · Viewed 34.1k times · Source

I am attempting to encode video using libavcodec/libavformat. Audio works great, but when I try to encode video I get the following errors:

[libx264 @ 0x10182a000]broken ffmpeg default settings detected  
[libx264 @ 0x10182a000]use an encoding preset (vpre)  

easy to fix using the command line ffmpeg, but I am trying to do this in C. my options are

AVStream *pVideoOutStream = av_new_stream(pOutFormatCtx, 0);  
AVCodecContext *pVideoOutCodecCtx  = pVideoOutStream->codec;  

pVideoOutCodecCtx->codec_id        = CODEC_ID_H264;    
pVideoOutCodecCtx->codec_type      = CODEC_TYPE_VIDEO;  
pVideoOutCodecCtx->bit_rate        = pVideoInCodecCtx->bit_rate;  
pVideoOutCodecCtx->width           = pVideoInCodecCtx->width;    
pVideoOutCodecCtx->height          = pVideoInCodecCtx->height;  
pVideoOutCodecCtx->pix_fmt         = pVideoInCodecCtx->pix_fmt;    
pVideoOutCodecCtx->sample_rate     = pVideoInCodecCtx->sample_rate;    
pVideoOutCodecCtx->gop_size        = 30;  

but avcodec_open() fails.

What other values do I need to set to make x264 happy?

Answer

Sergey Skopus picture Sergey Skopus · Sep 13, 2012

Don't forget to use x264 private options. You can always set a profile:

av_dict_set(&This->opts, "vprofile", "baseline", 0)

Or set the lowest encoding latency:

av_dict_set(&This->opts, "tune", "zerolatency", 0);

Or select a preset:

av_dict_set(&This->opts, "preset","ultrafast",0);

Before opening a codec

avcodec_open2(This->context, This->codec, &This->opts)