【问题标题】:Correct Use Of avcodec_encode_video2() Flush正确使用 avcodec_encode_video2() 刷新
【发布时间】:2016-05-14 22:51:13
【问题描述】:

我有一台相机将图片发送到回调函数,我想使用FFmpeg 用这些图片制作电影。我遵循了decoding_encoding 示例here,但不确定如何使用got_output 刷新编码器并获取延迟帧。

  1. 我是否应该在相机的所有图片到达时对其进行编码,然后当我想停止捕获并关闭视频时,我是否执行刷新循环?

或者

  1. 我是否应该定期进行刷新,比如说每收到 100 张照片?

我的视频捕获程序可能会运行数小时,所以我担心这种延迟帧在内存消耗中的作用,如果它们在刷新之前堆积在那里,这可能会占用我所有的内存。


这是示例执行的编码,它为 1 秒的视频生成 25 个虚拟 Frames,然后,最后,它循环通过 avcodec_encode_video2() 寻找 got_output 的延迟帧:

/////  Prepare the Frame, CodecContext and some aditional logic.....

/* encode 1 second of video */
for (i = 0; i < 25; i++) {
    av_init_packet(&pkt);
    pkt.data = NULL;    // packet data will be allocated by the encoder
    pkt.size = 0;
    fflush(stdout);
    /* prepare a dummy image */
    /* Y */
    for (y = 0; y < c->height; y++) {
        for (x = 0; x < c->width; x++) {
            frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
        }
    }
    /* Cb and Cr */
    for (y = 0; y < c->height/2; y++) {
        for (x = 0; x < c->width/2; x++) {
            frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
            frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
        }
    }
    frame->pts = i;
    /* encode the image */
    ret = avcodec_encode_video2(c, &pkt, frame, &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);
    }
}
/* 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);
    }
}

/////  Closes the file and finishes.....

【问题讨论】:

    标签: video ffmpeg h.264 video-encoding


    【解决方案1】:

    延迟是固定的,因此您的编码器延迟永远不会超过delay 帧。因此,内存消耗不会随着记录长度的增加而增加,因此没有问题,导致正确答案 1:您仅在编码结束时刷新。

    【讨论】:

    • 非常感谢罗纳德!我正在完成我的测试代码,一旦我测试了所有内容,我将接受你所有的答案!
    猜你喜欢
    • 2021-11-23
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2019-10-16
    • 2011-10-22
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多