简体   繁体   中英

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

I am trying to encode video in h.264 format using FFMpeg in iOS. Actually I am receiving sample buffer from iPhone Camera and then converting it AVFrame and further from AVFrame to H.264 video. But while h.264 encoding if I use :

codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);

Then codec is found but if I use:

codec = avcodec_find_encoder(CODEC_ID_H264);

Then codec is nil means codec not found. Full code is as below:

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

Your ffmpeg probably was compiled without libx264 support. So it really can't find encoder for H.264 as there are no other H.264 encoders in ffmpeg afaik.

For one thing, ffmpeg and x264 are licensed under the LGPL and GPL, so you would need to address license issues before you could include that code in your iPhone app. Under iOS, you would want to use the hardware h.264 encoder already supplied with the device. The AVAsset API is used to decode from and encode to h.264 in an iOS app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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