我尝试使用ffmpeg编码视频/但它在AVCodecContext和AVCodec的初始化时失败。 我做了什么:
_codec = avcodec_find_encoder(CODEC_ID_H264);
_codecContext = avcodec_alloc_context3(_codec);
_codecContext->coder_type = 0;
_codecContext->me_cmp|= 1;
_codecContext->me_method=ME_HEX;
_codecContext->me_subpel_quality = 0;
_codecContext->me_range = 16;
_codecContext->gop_size = 12;
_codecContext->scenechange_threshold = 40;
_codecContext->i_quant_factor = 0.71;
_codecContext->b_frame_strategy = 1;
_codecContext->qcompress = 0.5;
_codecContext->qmin = 2;
_codecContext->qmax = 31;
_codecContext->max_qdiff = 4;
_codecContext->max_b_frames = 3;
_codecContext->refs = 3;
_codecContext->trellis = 1;
_codecContext->width = format.biWidth;
_codecContext->height = format.biHeight;
_codecContext->time_base.num = 1;
_codecContext->time_base.den = 30;
_codecContext->pix_fmt = PIX_FMT_YUV420P;
_codecContext->chromaoffset = 0;
_codecContext->thread_count =1;
_codecContext->bit_rate = (int)(128000.f * 0.80f);
_codecContext->bit_rate_tolerance = (int) (128000.f * 0.20f);
int error = avcodec_open2(_codecContext, _codec, NULL);
if(error< )
{
std::cout<<"Open codec fail. Error "<<error<<"\n";
return NULL;
}
以这种方式,ii在avopen_codec2()上失败:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xae1fdb70 (LWP 30675)]
0xb2eb2cbb in x264_param_default () from /usr/lib/libx264.so.120
如果我评论所有AVCodecContext参数设置 - 我有:
[libx264 @ 0xac75edd0] invalid width x height (0x0)
avcodec_open重新调整负值。我正在做哪些步骤是错的?
感谢您的帮助(ffmpeg 0.10&amp;&amp; libx264昨日每日快照)
答案 0 :(得分:0)
根据我的经验,在初始化编解码器时,您应该向FFMPEG提供最少量的信息。这可能看似违反直觉,但这意味着FFMPEG将使用比您自己的猜测更有可能工作的默认设置。请参阅下面的内容:
AVStream *st;
m_video_codec = avcodec_find_encoder(AV_CODEC_ID_H264);
st = avformat_new_stream(_outputCodec, m_video_codec);
_outputCodecContext = st->codec;
_outputCodecContext->codec_id = m_fmt->video_codec;
_outputCodecContext->bit_rate = m_AVIMOV_BPS; //Bits Per Second
_outputCodecContext->width = m_AVIMOV_WIDTH; //Note Resolution must be a multiple of 2!!
_outputCodecContext->height = m_AVIMOV_HEIGHT; //Note Resolution must be a multiple of 2!!
_outputCodecContext->time_base.den = m_AVIMOV_FPS; //Frames per second
_outputCodecContext->time_base.num = 1;
_outputCodecContext->gop_size = m_AVIMOV_GOB; // Intra frames per x P frames
_outputCodecContext->pix_fmt = AV_PIX_FMT_YUV420P;//Do not change this, H264 needs YUV format not RGB
与前面的答案一样,这是一个将RGB帧编码为H264视频的FFMPEG库的工作示例:
http://www.imc-store.com.au/Articles.asp?ID=276
您的代码需要额外考虑:
您是否在下面调用了注册表?
avcodec_register_all();
av_register_all();
如果您在代码开头附近没有调用这两个函数,那么随后对FFMPEG的调用将会失败,您很可能会出现段错误。
看一下链接的例子,我在VC ++ 2010上进行了测试,结果很好。