2009-02-02 34 views
15

我試圖尋找一個使用ffmpeg的av_seek_frame方法的電影,但是我在確定如何生成一個時間標記來尋求最困難的。假設我想要向前或向後尋找x幀數量,並且我知道電影目前處於哪個幀,那麼我將如何去做這件事?ffmpeg av_seek_frame

+0

難道你不能使用幀頻計算時間偏移嗎? – 2009-02-02 20:51:40

+0

據我所知,時間偏移量需要以time_base爲單位,但我不確定如何將其轉換爲這些單位(或者即使這是我需要做的)。如果這是我需要做的,我不確定time_base的單位是什麼(秒,幀,每秒幀數)。 – 2009-02-02 20:56:10

回答

8

簡單的答案:你應該有一個AVFormatContext對象躺在周圍。它的duration屬性告訴你文件在時間戳乘以1000時可以在av_seek_frame中使用多長時間,因此將其視爲100%。然後,您可以計算您想要查找的視頻的距離。

如果你想前進一幀,只需調用av_read_frame和avcodec_decode_video,直到它用非零值填充got_picture_ptr。在調用avcodec_decode_video之前,請確保來自av_read_frame的數據包來自視頻流。然後avcodec_decode_video將填寫AVFrame結構,您可以使用它來做任何事情。

13

這是我如何做的:

// Duration of one frame in AV_TIME_BASE units 
int64_t timeBase; 

void open(const char* fpath){ 
    ... 
    timeBase = (int64_t(pCodecCtx->time_base.num) * AV_TIME_BASE)/int64_t(pCodecCtx->time_base.den); 
    ... 
} 

bool seek(int frameIndex){ 

    if(!pFormatCtx) 
     return false; 

    int64_t seekTarget = int64_t(frameIndex) * timeBase; 

    if(av_seek_frame(pFormatCtx, -1, seekTarget, AVSEEK_FLAG_ANY) < 0) 
     mexErrMsgTxt("av_seek_frame failed."); 

} 

的AVSEEK_FLAG_ANY使尋求每一幀,而不只是關鍵幀。