2016-02-05 47 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