问题:FFMPEG 使用 av_seek_frame 使用字节位置进行搜索

2021-12-26 00:00:00 ffmpeg c++ libavcodec

我试图让 av_seek_frame() 函数转到我指定的字节位置.我正在为我的应用程序实现帧精确搜索机制,按照我的看法,我将扫描整个视频文件,并将每个关键帧的字节位置存储在一个结构中.我找到了获取当前字节位置的位置:AVPacket.pos.我现在用 av_seek_frame 测试这个位置,如下所示:

I am trying to get the av_seek_frame() function to go to a byte position I specify. I am implementing a frame accurate seeking mechanism for my application, and the way I see it, I will scan the entire video file, and store byte positions for each keyframe in a struct. I found out where to get the current byte position: AVPacket.pos. I now test this position with av_seek_frame like this:

av_seek_frame( pFormatCtx, videoStream, 110285594, AVSEEK_FLAG_BYTE);

然而,这似乎不是正确的事情,当我调用 av_read_frame 时,它只是从第 23 帧开始.如果我不寻找,它会从第 1 帧开始.

However, this does not seem to do the right thing, when I call av_read_frame, it just starts with frame 23. If I do not seek, it starts at frame 1.

推荐答案

对于那些有兴趣的人,我找到了解决方案.经过数小时的谷歌搜索和一些简单的逆向工程,我找到了如何获取和设置打开视频的字节位置.

For those who are interested, I found the solution. After hours of googling and some simplistic form of reverse engineering, I found how to get and set the byte location of the open video.

获取文件位置:AVFormatContext.pb.pos

To get the file position: AVFormatContext.pb.pos

例如:

int64_t byteposition = pFormatCtx->pb->pos;

设置文件位置:url_seek(AVFormatContext.pb, Position, SEEK_SET);

To set the file position: url_seek(AVFormatContext.pb, Position, SEEK_SET);

例如:

url_seek(pFormatCtx->pb, 27909056, SEEK_SET);

如果您在玩游戏时更改位置,请不要忘记刷新缓冲区.如果在第一次做 av_read_frame 之前这样做,则不需要刷新.

Don't forget to flush the buffers if you change the location while playing. If you do this before you do av_read_frame for the first time, flushing is not necessary.

亲切的问候,尼克・维林登

Kind Regards, Nick Verlinden

相关文章