这是ffmpeg.c里的一段代码,功能是打开文件并绑定音视频流信息到AVFormatContext 这个结构体
- static int read_ffserver_streams(AVFormatContext *s, const char *filename)
- {
- int i, err;
- AVFormatContext *ic;
- /*打开文件,并将文件相关的信息存到ic中,filename为文件的完整路径*/
- err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
- if (err < 0)
- return err;
- /*复制文件里的流信息到输出的AVFormatContext结构体中,一般情况有2个流即nb_streams=2,
s->streams[0]为 视频流,s->streams[1]为音频流*/
- s->nb_streams = ic->nb_streams;
- for(i=0;i<ic->nb_streams;i++) {
- AVStream *st;
- /*修正:需要一个更优化的解决方案*/
- st = av_mallocz(sizeof(AVStream));
- memcpy(st, ic->streams[i], sizeof(AVStream));
- /*初始化编解码器结构体,将文件所用的编码格式存储到用于输出的AVFormatContext结构体中*/
- st->codec = avcodec_alloc_context();
- memcpy(st->codec, ic->streams[i]->codec, sizeof(AVCodecContext));
- s->streams[i] = st;
- }
- /*打开文件后需要关闭*/
- av_close_input_file(ic);
- return 0;
- }