objective-c - 无法使用 ffmpeg 对图像进行编码

标签 objective-c encoding ffmpeg

我正在尝试在 ffmpeg 库的帮助下对一张图像进行编码。实际上我想对实时视频进行编码,但现在我开始对图像进行编码。
这是我的代码..

    av_register_all();
    avcodec_init();
    avcodec_register_all();
    avformat_alloc_context();

    AVCodec *codec;
    AVCodecContext *ctx= NULL;
    int out_size, size, outbuf_size;
    AVFrame *picture;
    uint8_t *outbuf;
    unsigned char *flvdata = malloc(sizeof(unsigned char) * 30);


    outbuf_size = 100000;
    outbuf = malloc(outbuf_size);


    printf("Video encoding\n");

    codec = avcodec_find_encoder(CODEC_ID_FLV1);
    if (!codec) {
            fprintf(stderr, "codec not found\n");
            exit(1);
    }

    ctx= avcodec_alloc_context();
    picture= avcodec_alloc_frame();


    ctx->width = 320;
    ctx->height = 240;
    ctx -> sample_rate = 11025;
    ctx -> time_base.den = 1000;
    ctx -> time_base.num = 23976;
    ctx -> codec_id = CODEC_ID_FLV1;
    ctx -> codec_type = CODEC_TYPE_VIDEO;
    ctx->pix_fmt = PIX_FMT_YUV420P;

    if (avcodec_open(ctx, codec) < 0) {
            fprintf(stderr, "could not open codec\n");
            exit(1);
    }

    outbuf_size = 100000;
    outbuf = malloc(outbuf_size);
    size = ctx->width * ctx->height;

    AVFrame* outpic = avcodec_alloc_frame();
    int nbytes = avpicture_get_size(PIX_FMT_YUV420P, ctx->width, ctx->height);

    uint8_t* outbuffer = (uint8_t*)av_malloc(nbytes);

    fflush(stdout);

    int numBytes = avpicture_get_size(PIX_FMT_YUV420P, ctx->width, ctx->height);

    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"0.jpg"]];
    CGImageRef newCgImage = [image CGImage];

    CGDataProviderRef dataProvider = CGImageGetDataProvider(newCgImage);
    CFDataRef bitmapData = CGDataProviderCopyData(dataProvider);
    long dataLength = CFDataGetLength(bitmapData);

    uint8_t *buffer = (uint8_t *)av_malloc(dataLength);
    buffer = (uint8_t *)CFDataGetBytePtr(bitmapData);

    for(int i = 0; i < dataLength; i++)
    {
            if((i + 1) % 16 == 1 && i != 1)
                    printf("\n");
            printf("%X\t",buffer[i]); // getting something different than the     actual hex value of the image
    }



    outpic -> pts = 0;        

    avpicture_fill((AVPicture*)picture, buffer, PIX_FMT_RGB8, ctx->width, ctx->height);

    avpicture_fill((AVPicture*)outpic, outbuffer, PIX_FMT_YUV420P, ctx->width, ctx->height);

    struct SwsContext* fooContext = sws_getContext(ctx->width, ctx->height, 
                                                           PIX_FMT_RGB8, 
                                                           ctx->width, ctx->height, 
                                                           PIX_FMT_YUV420P, 
                                                           SWS_FAST_BILINEAR, NULL, NULL, NULL);

    sws_scale(fooContext, picture->data, picture->linesize, 0, ctx->height, outpic->data, outpic->linesize);

    printf("abcdefghijklmnop");
    out_size = avcodec_encode_video(ctx, outbuf, outbuf_size, outpic);
    printf("\n\n out_size %d   outbuf_size %d",out_size,outbuf_size);

所以图像的大小是 29 kb 并且 avcodec_encode_video 返回 20143 .. 那么它是否正确?我的意思是我正在编码,所以它的大小也应该减小......而且我已经以十六进制模式打开了该图像,并且我得到的数据与缓冲区中的数据不同(如代码所示)。所以我认为缓冲区没有得到正确的数据。正确的?
任何人都可以帮我处理我的代码吗?
先感谢您...

最佳答案

回答我自己的问题...

现在将 outbuf_size 增加到 200000 后,我可以对图像进行编码。问题是需要更大大小的缓冲区来对图像进行编码。
这解决了我的编码问题。

但是现在我遇到了另一个问题,因为编码的图像是黑白的。我想要彩色图像。请告诉我应该如何设置像素格式以获得彩色图像。请帮助......剩下的时间非常少。

提前致谢..

关于objective-c - 无法使用 ffmpeg 对图像进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10212358/

相关文章:

具有特定颜色的 FFMPEG 波形

ios - 无法在 TableView 单元格中显示来自解析的 imageView

objective-c - 递归 block 中的 ARC 行为

ios - 在 IOS 中将图像数组加载到 slider 中

html - 如何为 HtmlAgilityPack HtmlDocument 设置编码

javascript - 带有 JavaScript 的 ffmpeg 缩略图

objective-c - 具有特定位置的 MKMapItem

python - 语法错误 : encoding declaration in Unicode string

python - 用于 u""文字的编码

youtube - PHP 从 YouTube 视频中获取屏幕截图