使用FFmpeg进行H264编码需要在FFmpeg编译中引入libx264
初始化设置编码环境
- (BOOL)setEncode
{
avcodec_register_all();
_H264Codec = avcodec_find_encoder(CODEC_ID_H264);
if (_H264Codec == nil) {
NSLog(@"编解码器不支持");
return NO;
}
_codecContext = avcodec_alloc_context3(_H264Codec);
if (_codecContext == nil) {
NSLog(@"初始化编解码环境失败");
return NO;
}
_codecContext -> width = 640;
_codecContext -> height = 480;
_codecContext->pix_fmt = PIX_FMT_YUV420P;
if(avcodec_open2(_codecContext, _H264Codec, NULL) < 0) {
NSLog(@"打开编码器失败");
return NO;
}
return YES;
}如果avcodec_find_encoder
(
CODEC_ID_H264
)返回NULL且确定
在编译时已连接如libx264,检查编译时config中是否设置了--enable-encoder=libx264
编码
- (void)encodeWithData:(const void *)data andConfig:(YUVConfig)config andReslutBlock:(encodeBlock)reslut {
dispatch_sync(_encode

该博客详细介绍了如何在iOS平台上使用FFmpeg库将YUV420P格式的视频帧实时编码为H264格式。在FFmpeg编译时需包含libx264库。编码过程中,设置了关键帧的PTS以避免解码问题,并强调了编码解码过程需要在同步环境中进行,以确保数据一致性。提供了完整的H264编码代码示例。
3919

被折叠的 条评论
为什么被折叠?



