我正在尝试使用 iOS 中的 FFMpeg 以 h.264 格式编码视频。实际上,我正在从 iPhone 相机接收样本缓冲区,然后将其转换为 AVFrame 并进一步从 AVFrame 转换为 H.264 视频。但是如果我使用 h.264 编码:
codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
然后找到编解码器,但如果我使用:
codec = avcodec_find_encoder(CODEC_ID_H264);
然后编解码器为零表示未找到编解码器。完整代码如下:
static void encode(AVFrame *picture)
{
AVCodec *codec;
AVCodecContext *c= NULL;
int i, out_size, size, outbuf_size;
uint8_t *outbuf, *picture_buf;
printf("Video encoding\n");
/* find the mpeg1 video encoder */
//avcodec_init() ; // Also tried this but giving warning and not worked
avcodec_register_all();
codec = avcodec_find_encoder(CODEC_ID_H264);
if (!codec) {
fprintf(stderr, "codec not found\n");
exit(1);
}
c= avcodec_alloc_context();
picture= avcodec_alloc_frame();
/* put sample parameters */
c->bit_rate = 400000;
/* resolution must be a multiple of two */
c->width = 352;
c->height = 288;
/* frames per second */
c->time_base= (AVRational){1,25};
c->gop_size = 10; /* emit one intra frame every ten frames */
c->max_b_frames=1;
c->pix_fmt = PIX_FMT_YUV420P;
/* open it */
if (avcodec_open(c, codec) < 0) {
fprintf(stderr, "could not open codec\n");
exit(1);
}
/* alloc image and output buffer */
outbuf_size = 100000;
outbuf = malloc(outbuf_size);
size = c->width * c->height;
picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
NSLog(@"NSdada===%@",[NSData dataWithBytes:(const void *)outbuf length:out_size]);
free(picture_buf);
free(outbuf);
avcodec_close(c);
av_free(c);
av_free(picture);
printf("\n");
}