【问题标题】:ffmpeg library m4a moov atom not found when using custom IOContext使用自定义 IOContext 时找不到 ffmpeg 库 m4a moov atom
【发布时间】:2017-04-26 16:02:36
【问题描述】:

我目前正在尝试在 SFML 中实现 FFmpeg,因此我可以读取更广泛的音频文件,但在打开 m4a 文件时出现错误 [mov,mp4,m4a,3gp,3g2,mj2 @ #] moov atom not found。现在这只发生在我使用自定义 IOContext 读取文件而不是从 URL 打开它时。 This page 这里说我不应该使用流来打开 m4a 文件,但是 IOContext 是否被视为流?因为我无法将它作为 URL 打开,因为这就是 SFML 的工作原理。

// Explanation of InputStream class
class InputStream {
    int64_t getSize()
    int64_t read(void* data, int64_t size);
    int64_t seek(int64_t position);
    int64_t tell(); // Gets the stream position
};

// Used for IOContext
int read(void* opaque, uint8_t* buf, int buf_size) {
    sf::InputStream* stream = (sf::InputStream*)opaque;
    return (int)stream->read(buf, buf_size);
}
// Used for IOContext
int64_t seek(void* opaque, int64_t offset, int whence) {
    sf::InputStream* stream = (sf::InputStream*)opaque;
    switch (whence) {
    case SEEK_SET:
        break;
    case SEEK_CUR:
        offset += stream->tell();
        break;
    case SEEK_END:
        offset = stream->getSize() - offset;
    }
    return (int64_t)stream->seek(offset);
}

bool open(sf::InputStream& stream) {
    AVFormatContext* m_formatContext = NULL;
    AVIOContext* m_ioContext = NULL;
    uint8_t* m_ioContextBuffer = NULL;
    size_t m_ioContextBufferSize = 0;

    av_register_all();
    avformat_network_init();
    m_formatContext = avformat_alloc_context();

    m_ioContextBuffer = (uint8_t*)av_malloc(m_ioContextBufferSize);
    if (!m_ioContextBuffer) {
        close();
        return false;
    }
    m_ioContext = avio_alloc_context(
        m_ioContextBuffer, m_ioContextBufferSize,
        0, &stream, &::read, NULL, &::seek
    );
    if (!m_ioContext) {
        close();
        return false;
    }
    m_formatContext = avformat_alloc_context();
    m_formatContext->pb = m_ioContext;

    if (avformat_open_input(&m_formatContext, NULL, NULL, NULL) != 0) {
        // FAILS HERE
        close();
        return false;
    }

    //...

    return true;
}

【问题讨论】:

    标签: c++ ffmpeg m4a


    【解决方案1】:

    原来只有一个问题,就是我的搜索功能。显然 ffmpeg 有另一个可用的来源选项AVSEEK_SIZE。这是实现。之后它就可以工作了。

    int64_t seek(void* opaque, int64_t offset, int whence) {
        sf::InputStream* stream = (sf::InputStream*)opaque;
        switch (whence) {
        case SEEK_SET:
            break;
        case SEEK_CUR:
            offset += stream->tell();
            break;
        case SEEK_END:
            offset = stream->getSize() - offset;
            break;
        case AVSEEK_SIZE:
            return (int64_t)stream->getSize();
        }
        return (int64_t)stream->seek(offset);
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-20
      • 2019-09-17
      • 2019-06-28
      • 1970-01-01
      • 2020-06-25
      • 2019-09-21
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      相关资源
      最近更新 更多