使用ffmpeg将图像编码为视频

我正在尝试将一系列图像编码为一个视频文件。我正在使用来自api-example.c的代码,它的作品,但它在视频中给了我奇怪的绿色。我知道,我需要将我的RGB图像转换为YUV,我找到了一些解决方案,但它不起作用,颜色不是绿色但很奇怪,所以这就是代码:
    // Register all formats and codecs
    av_register_all();

    AVCodec *codec;
    AVCodecContext *c= NULL;
    int i, out_size, size, outbuf_size;
    FILE *f;
    AVFrame *picture;
    uint8_t *outbuf;

    printf("Video encodingn");

    /* find the mpeg video encoder */
    codec = avcodec_find_encoder(CODEC_ID_MPEG2VIDEO);
    if (!codec) {
        fprintf(stderr, "codec not foundn");
        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 codecn");
        exit(1);
    }

    f = fopen(filename, "wb");
    if (!f) {
        fprintf(stderr, "could not open %sn", filename);
        exit(1);
    }

    /* alloc image and output buffer */
    outbuf_size = 100000;
    outbuf = malloc(outbuf_size);
    size = c->width * c->height;

#pragma mark -
    AVFrame* outpic = avcodec_alloc_frame();
    int nbytes = avpicture_get_size(PIX_FMT_YUV420P, c->width, c->height);

    //create buffer for the output image
    uint8_t* outbuffer = (uint8_t*)av_malloc(nbytes);

#pragma mark -  
    for(i=1;i<77;i++) {
        fflush(stdout);

        int numBytes = avpicture_get_size(PIX_FMT_YUV420P, c->width, c->height);
        uint8_t *buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));

        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"10%d", i]];
        CGImageRef newCgImage = [image CGImage];

        CGDataProviderRef dataProvider = CGImageGetDataProvider(newCgImage);
        CFDataRef bitmapData = CGDataProviderCopyData(dataProvider);
        buffer = (uint8_t *)CFDataGetBytePtr(bitmapData);   

        avpicture_fill((AVPicture*)picture, buffer, PIX_FMT_RGB8, c->width, c->height);
        avpicture_fill((AVPicture*)outpic, outbuffer, PIX_FMT_YUV420P, c->width, c->height);

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

        //perform the conversion
        sws_scale(fooContext, picture->data, picture->linesize, 0, c->height, outpic->data, outpic->linesize);
        // Here is where I try to convert to YUV

        /* encode the image */
        out_size = avcodec_encode_video(c, outbuf, outbuf_size, outpic);
        printf("encoding frame %3d (size=%5d)n", i, out_size);
        fwrite(outbuf, 1, out_size, f);

        free(buffer);
        buffer = NULL;      

    }

    /* get the delayed frames */
    for(; out_size; i++) {
        fflush(stdout);

        out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
        printf("write frame %3d (size=%5d)n", i, out_size);
        fwrite(outbuf, 1, outbuf_size, f);      
    }

    /* add sequence end code to have a real mpeg file */
    outbuf[0] = 0x00;
    outbuf[1] = 0x00;
    outbuf[2] = 0x01;
    outbuf[3] = 0xb7;
    fwrite(outbuf, 1, 4, f);
    fclose(f);
    free(outbuf);

    avcodec_close(c);
    av_free(c);
    av_free(picture);
    printf("n");
请给我建议如何解决这个问题。     
已邀请:
你可以看文章http://unick-soft.ru/Articles.cgi?id=20。但它是关于俄语的文章,但它包括代码示例和VS示例。     
有人找到了解决这个问题的方法吗?我在解码端看到绿色视频问题。也就是说,当我解码传入的PIX_FMT_YUV420数据包然后将它们swsscale到PIX_FMT_RGBA。 谢谢! 编辑: 绿色图像可能是由于手臂优化回火。我用它来解决我的问题: http://ffmpeg-users.933282.n4.nabble.com/green-distorded-output-image-on-iPhone-td2231805.html 我想这个想法是不指定任何架构(配置会给你一个关于架构未知的警告,但你仍然可以继续“制造”)。这样,不使用手臂优化。可能会有轻微的性能损失(如果有的话),但至少它有效! :)     
我认为问题很可能是你使用
PIX_FMT_RGB8
作为输入像素格式。这并不意味着每通道8位,如常用的24位RGB或32位ARGB。这意味着每像素8位,这意味着所有三个颜色通道都位于一个字节中。我猜这不是你的图像格式,因为它非常罕见,所以你需要使用
PIX_FMT_RGB24
PIX_FMT_RGB32
,具体取决于你的输入图像是否有alpha通道。有关像素格式的信息,请参阅此文档页面。     

要回复问题请先登录注册