[英]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.