自定义 AVIOContext 的未定义 AVFormatContext 持续时间

Undefined AVFormatContext duration for custom AVIOContext

问题是为什么字段 AVFormatContext::duration 在

之后未定义
avformat_open_input
avformat_find_stream_info

如果像 here or here 那样使用自定义 AVIOContext。

通过简单的文件打开,设置了该字段。 Example

问题是我没有定义搜索函数,所以对于 std::istream 示例,这应该是

static int64_t seekFunction(void* opaque, int64_t offset, int whence)
{

auto& stream = *reinterpret_cast<std::istream*>(opaque);

if(whence == AVSEEK_SIZE)
{
    size_t position = stream.tellg();

    stream.seekg(0, std::ios::beg);
    size_t begin = stream.tellg();

    stream.seekg(0, std::ios::end);
    size_t end = stream.tellg();

    stream.seekg(position, std::ios::beg);

    return end - begin;
}

stream.seekg(offset, std::ios::beg);

return stream.tellg();

}

如果是 eof

,你还应该在 readFunction 中为 stream 添加 clear for clear for stream