You also should avcodec_flush_buffers after you seek to a new location that’s behind where you were reading from (you should probably just call it every time you seek, but I am not sure about performance). Notice that at this point we need to know the decoded frame format and dimensions (the width and height) to calculate the buffer size. However, it will only ever be behind the target timestamp, so you can just av_read_frame until you get to where you want, using AVframe->pts * AVStream->timebase to calculate what time the frame is (use av_rescale to do this).Īlso note that if you need to seek backward (that is a frame behind one you already read with av_read_frame) or you are going to be calling av_read_frame more than once on a frame in general, you have to send/receive the packet/frame with avcodec_send_packet and avcodec_receive_frame respectively, otherwise the codec context will be out of sync (I think that’s the issue?). AVFrame frame NULL, swframe NULL frame avframealloc()) swframe avframealloc() Allocate a linear buffer to store frame data for further use. This seeks to the nearest keyframe! Which could be pretty far from you want. Int err = avformat_seek_file(pFormatContext, video_stream_index, 0, ts, ts, 0) that we only look for nearest keyframes behind us I have set the max timestamp to the same value so AVSEEK_FLAG_ANY/FRAME/BACKWARD (BACKWARD is ignored anyways) it here is to seek to the nearest keyframe (not frame!). all use it.it is the most consistent and easiest to use. work in progress, it's been around for more than a decade now, ffplay/ffmpeg/ffprobe even though it mentions in docs that you shouldn't use this because it is a so just multiply by the reciprocal (den = denominator, num = numerator)įormat_ctx->streams->time_n,įormat_ctx->streams->time_base.num the time base may be 1/1000th of a second, Not sure if this is super accurate, but the following is very simple and seems to work: int n_seconds = 10 // seek forward 10 seconds
0 Comments
Leave a Reply. |