2016-02-05 48 views
2

我有一台摄像机发送图片到回调函数,我想用这张图片使用FFmpeg拍摄一部电影。我遵循decoding_encoding示例here,但我不确定如何使用got_output来清除编码器并获取延迟的帧。正确使用avcodec_encode_video2()刷新

  1. 768,16我编码所有我的相机的图片,当他们到达时,后来当我要停止捕获和关闭视频,我做的冲洗循环?

或者

  • 我应该做定期冲洗,让我们说,每收到100张图片?
  • 我的视频捕捉程序可以运行数个小时,所以我很担心这个帧延迟的内存消耗是如何工作的,如果他们堆叠起来,直到红晕,这可能需要我所有的记忆。


    这是由例如执行的编码,它使得25虚设Frames 1秒的视频,后来,在最后,它循环通过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..... 
    

    回答

    3

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

    +0

    非常感谢罗纳德!我正在完成我的测试代码,只要我测试了一切,我会接受你的答案! – mFeinstein