ios ffmpeg h264 encode
2017-09-01 10:53
225 查看
简单封装了下,ffmepg需要包含libx264。
#import "FFMpegAVCEncode.h" #ifdef __cplusplus extern "C" { #endif #include <libavutil/opt.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> #ifdef __cplusplus }; #endif @implementation FFMpegAVCEncode { AVCodecContext *_pCodecCtx; AVCodec *_pCodec; AVPacket _pkt; uint8_t *_pictureBuf; AVFrame *_pFrame; int _ySize; int _frameCnt; int _frameWidth; // 编码的图像宽度 int _frameHeight; // 编码的图像高度 } - (int)initEncoder:(int)width height:(int)height bitrate:(int)bitrate { _frameCnt = 0; _frameWidth = width; _frameHeight = height; avcodec_register_all(); enum AVCodecID codec_id=AV_CODEC_ID_H264; _pCodec = avcodec_find_encoder(codec_id); if (!_pCodec) { printf("Codec not found\n"); return -1; } _pCodecCtx = avcodec_alloc_context3(_pCodec); if (!_pCodecCtx) { printf("Could not allocate video codec context\n"); return -1; } _pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO; _pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; _pCodecCtx->width = _frameWidth; _pCodecCtx->height = _frameHeight; _pCodecCtx->time_base.num = 1; _pCodecCtx->time_base.den = 15; _pCodecCtx->bit_rate = bitrate; _pCodecCtx->gop_size = 10; // H264 // pCodecCtx->me_range = 16; // pCodecCtx->max_qdiff = 4; // pCodecCtx->qcompress = 0.6; _pCodecCtx->qmin = 10; _pCodecCtx->qmax = 51; // Optional Param _pCodecCtx->max_b_frames=1; // Set Option AVDictionary *param = 0; // H.264 if(_pCodecCtx->codec_id == AV_CODEC_ID_H264) { av_dict_set(¶m, "preset", "slow", 0); av_dict_set(¶m, "tune", "zerolatency", 0); // av_dict_set(¶m, "profile", "main", 0); } if (!_pCodec) { printf("Can not find encoder! \n"); return -1; } int ret = 0; if ((ret = avcodec_open2(_pCodecCtx, _pCodec,¶m)) < 0) { printf("Failed to open encoder! \n"); return -1; } _pFrame = av_frame_alloc(); avpicture_fill((AVPicture *)_pFrame, _pictureBuf, _pCodecCtx->pix_fmt, _pCodecCtx->width, _pCodecCtx->height); _ySize = _pCodecCtx->width * _pCodecCtx->height; return 0; } -(void)encodeBufToH264:(char *)yuv420p{ _pictureBuf = yuv420p; _pFrame->data[0] = _pictureBuf; // Y _pFrame->data[1] = _pictureBuf+ _ySize; // U _pFrame->data[2] = _pictureBuf+ _ySize*5/4; // V // PTS _pFrame->pts = _frameCnt; int got_picture = 0; // Encode _pFrame->width = _frameWidth; _pFrame->height = _frameHeight; _pFrame->format = AV_PIX_FMT_YUV420P; int ret = avcodec_encode_video2(_pCodecCtx, &_pkt, _pFrame, &got_picture); if(ret < 0) { printf("Failed to encode! \n"); } if (got_picture==1) { printf("Succeed to encode frame: %5d\tsize:%5d\n", _frameCnt, _pkt.size); //TODO:h264数据回调 _frameCnt++; av_free_packet(&_pkt); } } - (void)free { //Flush Encoder int ret = [self flushEncoder]; if (ret < 0) { printf("Flushing encoder failed\n"); } avcodec_close(_pCodecCtx); av_free(_pCodecCtx); av_freep(&_pFrame->data[0]); av_frame_free(&_pFrame); } -(int)flushEncoder { int ret; int got_frame; AVPacket enc_pkt; while (1) { enc_pkt.data = NULL; enc_pkt.size = 0; av_init_packet(&enc_pkt); ret = avcodec_encode_video2(_pCodecCtx, &enc_pkt, NULL, &got_frame); av_frame_free(NULL); if (ret < 0) break; if (!got_frame){ ret=0; break; } //TODO:h264数据回调 printf("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n",enc_pkt.size); } return ret; } @end
相关文章推荐
- ios ffmpeg h264 decode
- iOS ffmpeg将音视频写入文件aac+h264
- iOS FFmpeg实时YUV420P编码H264
- ios一步一步编译ffmpeg,x264实现h264编码
- iOS平台使用ffmpeg解码h264视频流
- 在iOS平台使用ffmpeg解码h264视频流
- ios ffmpeg(libfdk-aac) aac encode
- ios 6.1下ffmpeg真机库带h264编码解码
- ios ffmpeg 实时视频压缩(主要是H264)
- 来源于 原创 分类 ios 5.0 ffmpeg decode h264 stream
- 在iOS平台使用ffmpeg解码h264视频流
- ios一步一步编译ffmpeg,x264实现h264编码
- ios一步一步编译ffmpeg,x264实现h264编码
- 在iOS平台使用ffmpeg解码h264视频流
- 在iOS平台使用ffmpeg解码h264视频流
- FFmpeg在iOS上完美编译
- FFMPEG_avi转码到mp4(aac+h264)源码
- [置顶] hls直播实现源码V2(h264,aac流输入,ffmpeg实现,MFC,VC环境,ts,m3u8)
- 关于ios中编译ffmpeg0.9.2库
- iOS - FFMPEG 学习(2) - 在application中使用ffmpeg库