使用libavcodec(ffmpeg)将一系列图像写入视频文件

时间:2013-11-19 01:15:17

标签: c ffmpeg video-streaming

要求:

我有一堆图像(更具体地说,它们是1024x768,24bpp RGB PNG文件),我想将其编码成视频文件。

我需要使用'libavcodec'库,而不是'ffmpeg'工具。 (我知道它们在起源上基本相同,我强调因为有人可能会回答使用'ffmpeg'工具,但这不是我正在寻找的解决方案)

我正在使用h264编码器。

目标: 具有相同分辨率(1024 x 768)的高质量视频,YUV420P 每张图片的持续时间为1秒。 24 fps

问题: 我已尝试过许多不同(但分辨率和位数相同)的png图像,并且都无法输出好的视频。

对于某些系列的图像,只有第一秒的帧以良好的形状显示,但其余的帧失真并且颜色改变(更亮)。

对于某些系列的图像,似乎图像被放大并再次扭曲。

等等。

问题: 我是一个完整的AV新手,我需要有人来验证我的编码步骤。我是AV新手。

1) av_register_all()

2) avcodec_register_all()

3) avcodec_find_encoder()

4) avcodec_alloc_context3()

5) sets codec configuraton to context.

6) avcodec_open2()

7) opens a output file using fopen_s()

8)

for(int second=1; second<=10; ++seconds)
{

  Read a image from local using Gdiplus

  Create a gdiplus bitmap and draw the image onto this bitmap

  Get the raw byte data using LockBits

  Transfer this RGB raw byte into YUV420 frame using 'swscontext', 'sws_scale'

  for(int f=0; f<24; ++f)
  {
    av_init_packet(&pkt);
    pkt.data = NULL;
    pkt.size = 0;

    pFrame->pts = f;
    ret = avcodec_encode_video2(pContext, &pkt, pFrame, &got_output);

    if(got_output)
    {
        fwrite(pkt.data, 1, pkt.size, outputFile);
        av_free_packet(&pkt);
    }
  }
}

/* get the delayed frames */
for (got_output = 1; got_output; i++) {
    fflush(stdout);

    ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
    if (ret < 0) {
        fprintf(stderr, "Error encoding frame\n");
        exit(1);
    }

    if (got_output) {
        printf("Write frame %3d (size=%5d)\n", i, pkt.size);
        fwrite(pkt.data, 1, pkt.size, f);
        av_free_packet(&pkt);
    }
}

close everything required

我确信我误解了使用ffmpeg api的一些步骤。上述伪代码基于ffmpeg的“编码”示例。哪一部分我做错了?请有人帮帮我吗?

抱歉英语破碎了。英语不是我的natvie语言。我尽力了= P

0 个答案:

没有答案