c++ - 将数据写入内存而不是文件时,avformat_write_header() 不起作用

标签 c++ c ffmpeg libavformat

我想从内存到内存重新采样给定的输入格式,到目前为止一切都很好。
但是当试图从 ffmpeg 获取输出头时它不起作用。
在这里,我分配上下文并传递 write_buffer 函数指针,这样它就不会写入文件,而是会使用所需的数据调用我的函数

unsigned char * aviobuffer = (unsigned char *) av_malloc (32768);
AVIOContext * avio = avio_alloc_context (aviobuffer, 32768,1, NULL, NULL, write_buffer, NULL);

AVFormatContext* containerContext;
avformat_alloc_output_context2(&containerContext, NULL, "s16le", NULL);
containerContext->pb = avio;
这是我的 write_buffer 函数
std::vector<char>* data;
int write_buffer(void *opaque, uint8_t *buf, int buf_size)
{
    std::vector<char> tmp;
    tmp.assign(buf, buf + buf_size);
    data->insert(data->end(), tmp.begin(), tmp.end());
    return buf_size;
}
现在,当我调用 avformat_write_header() 时,它不会调用我的 write_buffer() 函数 + 它返回 0,这意味着成功。
int ret = avformat_write_header(containerContext, NULL);
之后,我调用适当的函数来获取数据体本身,并且我的 write_buffer() 被正常调用,所以我现在只剩下没有标题的数据体了!
无论如何,我怎样才能获得输出标题?

最佳答案

好吧,经过大量调试后,我发现了 ffmpeg 写入格式头的方式。
长话短说,某些格式与编写标题的特殊功能相关联。"s16le" ffmpeg 中的格式与一个无关。但令人惊讶的是,正如我在问题 ffmpeg 可以写入其数据体但没有标题!
所以我搜索了一种接近我想要的格式并支持编写它的标题。我找到了"wav"格式,试过了,效果很好。幸运的是 wav 的默认值是 s16le,这正是我想要的。
所以总之我改变了这行代码

avformat_alloc_output_context2(&containerContext, NULL, "s16le", NULL);
avformat_alloc_output_context2(&containerContext, NULL, "wav", NULL);

关于c++ - 将数据写入内存而不是文件时,avformat_write_header() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64790113/

相关文章:

ffmpeg - php exec ffmpeg如何获取错误

c - 搜索图像模式

php - 如何在 PHP 中运行 ffmpeg 命令

c++ - Lua 将表传递给函数,修改它并在 C++ 中取回它

android - 尽管已正确保存,但文件没有出现在 SD 卡上;出现在设备上的文件资源管理器中

c - Scanf while循环和EOF处理

c - "Resetting"链表的"Head"指针

ffmpeg 色键过滤器 : blend parameter does not what it should

c++ - 如何修复 'Vcpkg/CMake Is Unable to determine target architecture'(Windows10、vcpkg、clion、cmake)

c++ - 从图像数组创建单个图像