Search code examples
c++ffmpeglibavcodeclibav

Undefined AVFormatContext duration for custom AVIOContext


The question is why field AVFormatContext::duration is undefined after

avformat_open_input
avformat_find_stream_info

if used custom AVIOContext like in here or here.

With simple file opening, this field is set. Example


Solution

  • The problem was that I not defined seek function so for std::istream example this should be

    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();
    
    }
    

    And you should also add clear for clear for stream in readFunction in case of eof