繁体   English   中英

在iOS中使用FFMPEG进行H.264编码时找不到编解码器

[英]Codec not found while H.264 encoding using FFMPEG in iOS

我正在尝试在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);

然后编解码器为nil表示找不到编解码器。 完整代码如下:

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");
 }

您的ffmpeg可能是在没有libx264支持的情况下编译的。 因此它真的找不到H.264编码器,因为ffmpeg afaik中没有其他H.264编码器。

一方面,ffmpeg和x264是根据LGPL和GPL许可的,因此您需要解决许可问题,然后才能在iPhone应用程序中包含该代码。 在iOS下,您将要使用设备随附的硬件h.264编码器。 AVAsset API用于在iOS应用中对h.264进行解码和编码。

暂无
暂无

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

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