简体   繁体   English

FFMPEG与x264编码

[英]FFMPEG with x264 encoding

I'm trying ton encode video from set of jpeg images to h264, using ffmpeg + x264 for it. 我正在尝试使用ffmpeg + x264将jpeg图像集中的视频编码为h264。 I init AVCodecContext in such way: 我以这种方式初始化AVCodecContext:

_outputCodec = avcodec_find_encoder(AV_CODEC_ID_H264);
_outputCodecContext = avcodec_alloc_context3(_outputCodec);
avcodec_get_context_defaults3(_outputCodecContext, _outputCodec);
_outputCodecContext->width                 = _currentWidth;
_outputCodecContext->height                = _currentHeight;
_outputCodecContext->pix_fmt               = AV_PIX_FMT_YUV420P;
_outputCodecContext->time_base.num         = 1;
_outputCodecContext->time_base.den         = 25;
_outputCodecContext->profile =FF_PROFILE_H264_BASELINE;
_outputCodecContext->level = 50;

avcodec_open return no errors, anything is OK, but when I call avcodec_encode_video2() I get such messages (I think it's from x264): avcodec_open不返回错误,一切正常,但是当我调用avcodec_encode_video2()时,我收到了这样的消息(我认为这是来自x264的消息):

using mv_range_thread = %d

%s

profile %s, level %s

And then app crashs. 然后应用崩溃。 My be there are more neccessary settings for codec context, when use x264 && 当使用x264 &&时,是否有更多必要的编解码器上下文设置

Without a full version of your code it is hard to see what the actual problem is. 没有完整版本的代码,很难看到实际的问题是什么。

Firstly here is a working example of the FFMPEG library encoding RGB frames to a H264 video: 首先,这是FFMPEG库的工作示例,该库将RGB帧编码为H264视频:

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

You could expand on this example by using CImage to load in your JPGs and pass the RGB data to the FFMPEG Class to encode to a H264 video. 您可以通过使用CImage加载JPG并将RGB数据传递到FFMPEG类以编码为H264视频,来扩展此示例。

A few thoughts on your example though: 不过,对您的示例有一些想法:

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

     avcodec_register_all(); av_register_all(); 
  • Also I'd re-write your code to be something like 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 
  • And then you need to convert the RGB JPG picture to the YUV format using swscale as pogorskiy said. 然后,您需要按照pogorskiy所说,使用swscale将RGB JPG图片转换为YUV格式。

Have a look at the linked example, I tested it on VC++2010 and it works perfectly and you can send it an RGB char array. 看一下链接的示例,我在VC ++ 2010上对其进行了测试,它可以完美工作,您可以向其发送RGB char数组。

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

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