FFmpeg 提供了多种输入方式,其中之一是通过内存输入。内存输入允许你直接从内存中读取数据,而不是从文件或网络流中读取。以下是如何使用 FFmpeg 的内存输入的步骤:1. **准备输入数据**:首先,你需要准备要输入的数据,这可以是任何格式的数据,比如
摘要:[[N_FFmpeg]] 实例 内存输入输出 ffmpeg内存编解码 内存输入 关键点就两个: 初始化AVIOContext时, 指定自定义的回调函数指针 AVIOContext中的缓存 unsigned char* _iobuffe
[[N_FFmpeg]]
实例 内存输入/输出
ffmpeg内存编解码
内存输入
关键点就两个:
初始化AVIOContext时, 指定自定义的回调函数指针
//AVIOContext中的缓存
unsigned char* _iobuffer = (unsigned char*)av_malloc(40690);// 注意缓存容器要足够大
AVIOContext* avio = avio_alloc_context(_iobuffer , 40690 , 0 , this, fill_buffer,nullptr,nullptr);
assert( avio != NULL);
pFormatCtx->pb = avio;//给AVFormatContext
if(avformat_open_input(&pFormatCtx,NULL,NULL,NULL)!=0){
printf("Couldn't open inputstream.(无法打开输入流)\n");
return -1;
}
回调函数
///注意缓存 容器 要足够大
int fill_buffer(void * opaque,uint8_t *buf, int bufsize){
SDKSource2Player *instance = static_cast<SDKSource2Player*>( opaque);
callback_data * data = instance->source2->takeDataPacketSync();
memcpy( buf, data->data, data->size);//复制到容器 给 ffmpeg
int size = data->size;
SDKSource2::destoryDataPacket(data);//销毁
return size;
}
内存输出
回调函数
//内存输出数据
int fill_buffer_out(void * opaque, uint8_t *buf, int bufsize){
WebSocketPlayer *instance = static_cast<WebSocketPlayer*>( opaque);
instance->onAVPacketOutData(buf,bufsize );
return bufsize;
}
分配
// 内存输出
unsigned char* out_buffer = (unsigned char*)av_malloc(40690);//~40k
AVIOContext* out_avio = avio_alloc_context(out_buffer , 40690 , 1 , this, nullptr,nullptr,nullptr);
assert( out_avio != NULL);
AVOutputFormat *oformat = av_guess_format("mp4", nullptr, nullptr);
ret = avformat_alloc_output_context2(&outAvFormatCtx,oformat,nullptr,nullptr);
outAvFormatCtx->pb = out_avio;
outAvFormatCtx->flags = AVFMT_FLAG_CUSTOM_IO;
踩坑指南
内存输出的时候需要注意 avio_alloc_context 的第三个参数 write_flag=1 表示 可写
输出 AVFormatContext 的flag 需要标记为 AVFMT_FLAG_CUSTOM_IO
...
outAvFormatCtx->pb = out_avio;
outAvFormatCtx->flags = AVFMT_FLAG_CUSTOM_IO;
...
打开输出avio_open的时候, 第二参数不能是 NULL, 即使没有什么意义; ~~貌似自定义输出,不调用也可以 (实测) ~~
avio_open(&outAvFormatCtx->pb, "nothing",AVIO_FLAG_WRITE) ;
if (!outAvFormatCtx->pb ) {
qDebug()<<"错误: format->pd is NULL";
throw ("Error open
