在读取位于使用libavformat内存文件 [英] Reading a file located in memory with libavformat

查看:172
本文介绍了在读取位于使用libavformat内存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在试图读取从服务器发送小的视频文件

I'm currently trying to read small video files sent from a server

为了读取使用了libavformat文件,你应该叫

In order to read a file using libavformat, you are supposed to call

av_open_input_file(&avFormatContext, "C:\\path\\to\\video.avi", 0, 0, 0);

的问题是,在这种情况下,该文件不是在磁盘上,但在存储器

The problem is that in this case the file is not on the disk, but in memory.

我在做什么的时刻正在下载文件,使用临时的名字写它在磁盘上,然后调用 av_open_input_file 的临时文件的名称,是不是一个很干净的解决方案。

What I'm doing for the moment is downloading the file, writing it on the disk using a temporary name, and then calling av_open_input_file with the temporary file name, which is not a very clean solution.

其实我要的是像 av_open_custom功能(安培; avFormatContext,&安培; myReadFunction,&安培; mySeekFunction); ,但我没有找到任何文档中。
我想这在技术上是可行的,因为该文件的名称是不是可以帮助图书馆确定它使用的格式。

In fact what I want is a function like av_open_custom(&avFormatContext, &myReadFunction, &mySeekFunction); but I didn't find any in the documentation. I guess it is technically possible, since the name of the file is not something that helps the library determine which format it is using.

那么,有没有这样的功能,或者av_open_input_file替代?

So is there a function like this, or an alternative to av_open_input_file?

推荐答案

这很有趣,我总能找到我自己的解决方案,之后我张贴在本网站的问题,虽然我一直在这个问题上几个小时。

It's funny how I always find the solution by myself right after I post the question on this site, even though I've been working on this problem for hours.

在事实上,你必须初始化 avFormatContext-> PB 之前调用 av_open_input ,并传递给它一个假的文件名。
这不是写在文件中,但在库的源$ C ​​$ C直接的评论。

In fact you have to initialize avFormatContext->pb before calling av_open_input, and pass to it a fake filename. This is not written in the documentation but in a commentary directly in the library's source code.

举例code,如果你想从一个IStream加载(未经测试,只是让别人有同样的问题可以得到的想法)

Example code if you want to load from an istream (untested, just so somebody which has the same problem can get the idea)

static int readFunction(void* opaque, uint8_t* buf, int buf_size) {
    auto& me = *reinterpret_cast<std::istream*>(opaque);
    me.read(reinterpret_cast<char*>(buf), buf_size);
    return me.gcount();
}

std::ifstream stream("file.avi", std::ios::binary);

const std::shared_ptr<unsigned char> buffer(reinterpret_cast<unsigned char*>(av_malloc(8192)), &av_free);
const std::shared_ptr<AVIOContext> avioContext(avio_alloc_context(buffer.get(), 8192, 0, reinterpret_cast<void*>(static_cast<std::istream*>(&stream)), &readFunction, nullptr, nullptr), &av_free);

const auto avFormat = std::shared_ptr<AVFormatContext>(avformat_alloc_context(), &avformat_free_context);
auto avFormatPtr = avFormat.get();
avFormat->pb = avioContext.get();
avformat_open_input(&avFormatPtr, "dummyFilename", nullptr, nullptr);

这篇关于在读取位于使用libavformat内存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆