ffmpeg av_seek_frame

2019-03-12 20:52发布

问题:

I am attempting to seek in a movie using ffmpeg's av_seek_frame method however I'm having the most trouble determining how to generate a time-stamp to seek to. Assuming I want to seek x amount of frames either forward or backward and I know what frame the movie is currently on, how would I go about doing this?

回答1:

Here is how i did it:

// 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.");

}

The AVSEEK_FLAG_ANY enables seeking to every frame and not just keyframes.



回答2:

Simple answer: You should have an AVFormatContext object lying around. Its duration property tells you how long your file is in terms of the time-stamp multiplied by 1000 that can be used in av_seek_frame, so treat it as 100%. Then you can calculate how far into the video you want to seek to.

if you want to go forward one frame, simply call av_read_frame and avcodec_decode_video until it fills the got_picture_ptr with a non-zero value. Before calling avcodec_decode_video make sure the packet from av_read_frame is from the video stream. avcodec_decode_video will then fill in the AVFrame structure which you can use to do anything with.



标签: c ffmpeg