简体   繁体   English

如何使用x264进行ffmpeg编码?

[英]How to use x264 for encoding with ffmpeg?

I tryed to use ffmpeg for encoding video/ But it fails on initialization of AVCodecContext annd AVCodec. 我尝试使用ffmpeg编码视频/,但是在初始化AVCodecContext和AVCodec时失败。 What I do: 我所做的:

_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;    
}

In such way ii fails on avopen_codec2() with: 这样,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

If i comment all AVCodecContext parameters settins - I have : 如果我注释所有AVCodecContext参数settins-我有:

[libx264 @ 0xac75edd0] invalid width x height (0x0)

And avcodec_open retunrs negative value. 并且avcodec_open会重新调整负值。 Which steps, I'm doing, are wrong? 我正在执行的哪些步骤是错误的?

Thanks for any help (ffmpeg 0.10 && libx264 daily snapshot for yesterday) 感谢您的帮助(昨天的ffmpeg 0.10 && libx264每日快照)

In my experience you should give FFMPEG the least amount of information when initialising your codec as possible. 以我的经验,在初始化编解码器时,应该为FFMPEG提供最少的信息。 This may seem counter intuitive but it means that FFMPEG will use it's default settings that are more likely to work than your own guesses. 这看起来似乎很违反直觉,但这意味着FFMPEG将使用它的默认设置,该默认设置比您自己的猜测更有可能起作用。 See what I would include below: 请参阅以下内容:

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

As in previous answers, here is a working example of the FFMPEG library encoding RGB frames to a H264 video: 如先前的答案,这是将RGB帧编码为H264视频的FFMPEG库的工作示例:

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

An extra thought on your code though: 不过,对代码的额外考虑是:

  • Have you called register all like below? 您是否已致电注册,如下所示?

    avcodec_register_all(); avcodec_register_all();

    av_register_all(); av_register_all();

    If you don't call these two functions near the start of your code your subsequent calls to FFMPEG will fail and you'll most likely seg-fault. 如果您在代码的开头附近没有调用这两个函数,则随后对FFMPEG的调用将失败,并且很可能会出现段错误。

Have a look at the linked example, I tested it on VC++2010 and it works perfectly. 看看链接的示例,我在VC ++ 2010上对其进行了测试,它可以完美运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM