使用ffmpeg将图像编码为视频

4

我正在尝试将一系列图像编码成一个视频文件。我正在使用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 encoding\n");

    /* find the mpeg video encoder */
    codec = avcodec_find_encoder(CODEC_ID_MPEG2VIDEO);
    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);
    }

    f = fopen(filename, "wb");
    if (!f) {
        fprintf(stderr, "could not open %s\n", 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");

请给我建议如何解决这个问题。

1个问题。上面的代码需要什么框架? - aryaxt
需要使用FFmpeg库 http://www.ffmpeg.org/。 - Steve
我也遇到了同样的问题... 你能否请帮我提供一下你的解决方案... - Ganesh
3个回答

3

1

有人解决了这个问题吗?我在解码端看到了绿色视频问题。也就是说,当我解码传入的PIX_FMT_YUV420数据包,然后将它们转换为PIX_FMT_RGBA时。

谢谢!

编辑: 绿色图像可能是由于ARM优化反弹导致的。我在我的情况下使用了这个来解决问题: http://ffmpeg-users.933282.n4.nabble.com/green-distorded-output-image-on-iPhone-td2231805.html

我想关键是不指定任何架构(配置会警告架构未知,但您仍然可以继续“制作”)。这样,就不会使用ARM优化。可能会有轻微的性能损失(如果有的话),但至少它可以工作! :)


0

我认为问题最可能是您正在使用PIX_FMT_RGB8作为输入像素格式。这并不意味着每个通道都有8位,就像常用的24位RGB或32位ARGB一样。它意味着每个像素有8位,这意味着所有三个颜色通道都存储在单个字节中。我猜测这不是您图像的格式,因为它非常不常见,所以您需要使用PIX_FMT_RGB24PIX_FMT_RGB32,具体取决于您的输入图像是否具有alpha通道。请参阅this documentation页面了解有关像素格式的信息。


谢谢。我尝试过PIX_FMT_RGB24和PIX_FMT_RGB32以及许多其他格式。我使用了PIX_FMT_RGB8,因为我的图像针对网络进行了优化,它是索引了8位颜色的PNG和其他Photoshop文件,以使其更小。但我尝试过其他通常格式的图像,只是为了检查,使用不同的PIX_FMT选项,它仍然给我错误的结果,所以我认为问题出在代码上。 - Steve
你尝试过使用PIX_FMT_PAL8吗?该格式适用于8位索引颜色。使用PIX_FMT_PAL8时,必须随帧发送一个调色板。我提供的文档页面在定义所有像素格式之前讨论了它。它说AVFrame.data [0]包含每个像素的调色板索引,而AVFrame.data [1]包含一个1024字节的数组,定义了每个8位索引的实际ARGB颜色值。 - Jason B

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接