当尝试在200帧处搜索时,代码始终返回4。但是第四帧的结果似乎不对。我错在哪里?我的时基转换到实际帧有问题吗?
使用ffprobe的测试结果
Filename = test.mp4
Duration = 00:00:10.56
Fps = 25
Total frames = 256
The key frames pkt_pts_time are at 2.120000 and 0.000000 (using -skip_frame nokey )
Corresponding pkt_duration_time: 0.040000 and 0.040000 ( same, why?)
// Objective: seek to the nearest key frame
frameIndex = 200;
int64_t timeBase = (int64_t(pCodecCtx->time_base.num) * AV_TIME_BASE) / int64_t(pCodecCtx->time_base.den);
int64_t seekTarget = int64_t(frameIndex) * timeBase;
if (av_seek_frame(pFormatCtx, -1, seekTarget, AVSEEK_FLAG_FRAME | AVSEEK_FLAG_BACKWARD) < 0) return -1;
//convert the time_base to actual frame
auto time2frame = [&](int64_t tb) {
return tb * int64_t(pCodecCtx->time_base.den) / (int64_t(pCodecCtx->time_base.num) * AV_TIME_BASE);
};
AVPacket avPacket;
int result = av_read_frame(pFormatCtx, &avPacket);
if (result == 0) {
auto dts = avPacket.dts;
auto pts = avPacket.pts;
auto idx = avPacket.stream_index;
auto f = time2frame(pts); // expecting the actual frame here
std::cout << dts << pts << idx << f;
}