一般ffmpeg都是直接从文件中读取或者从网络流中读取,比如rtp://xx.xx.xx.xx:xxxx。
事实上也支持从内存中获取。
函数avio_alloc_context()实现该功能。
AVIOContext *avio_alloc_context(
unsigned char *buffer,
int buffer_size,
int write_flag,
void *opaque,
int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),//重写该函数,指定从内存中读取的方法,将buf_size字节大小的数据保存到buf
int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),//对应的这是写内存的函数
int64_t (*seek)(void *opaque, int64_t offset, int whence));
对于探测网络媒体流个格式,也可以用此种方法,先接收数据,然后探测。
下面贴出代码:
/*
*author tongli
*/
extern "C"{
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
#include "libavutil/avutil.h"
}
#define BUF_SIZE 4096*500
FILE* file;
//实现read_packet函数,从文件中读取模拟的是从内存中获取,同样可以实现为接收网络流
int read_packet(void *opaque, uint8_t *buf, int buf_size)
{
int n = 0;
if (!feof(file)){
n = fread(buf, 1, buf_size, file);
return n;
}else
return -1;
}
int ma