函数声明如下,int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags),
#define AVSEEK_FLAG_BACKWARD 1 ///< seek backward
#define AVSEEK_FLAG_BYTE 2 ///< seeking based on position in bytes
#define AVSEEK_FLAG_ANY 4 ///< seek to any frame, even non-keyframes
#define AVSEEK_FLAG_FRAME 8 ///< seeking based on frame number
代码
int f1 = 0;
const AVInputFormat* piFmt = NULL;
fmt_ctx = avformat_alloc_context();
ret = avformat_open_input(&fmt_ctx, "frag_bunny.mp4", piFmt, NULL);
while (true)
{
Sleep(6);
if (f1 == 0)
{
avformat_seek_file(fmt_ctx, 1, INT64_MIN, 40 * AV_TIME_BASE, INT64_MAX, AVSEEK_FLAG_FRAME);
f1++;
}
ret = av_read_frame(fmt_ctx, &pkt);
if (ret >= 0)
{
if (pkt.stream_index == video_stream_idx)
{
ret = decode_packet(video_dec_ctx, &pkt);
}
else if (pkt.stream_index == audio_stream_idx)
{
decodeAudio2Memory(audio_dec_ctx, &pkt, decoded_frame);
}
av_packet_unref(&pkt);
if (ret < 0)
{
continue;
}
}
}
用frag_bunny.mp4文件测试seek, 40 * AV_TIME_BASE是定位到40s,但时间好像对应不上,确实跳转了,但并非在第40s,AVSEEK_FLAG_BACKWARD和AVSEEK_FLAG_FRAME可以解码显示,
另外两个不可以