FFMPEG结构体分析一:AVFormatContext

来源:互联网 发布:pmp考试 知乎 编辑:程序博客网 时间:2024/05/17 13:12

一、概述

       关于AVFormatContext的分析已经有一篇文章了,地址如下:  http://blog.csdn.net/leixiaohua1020/article/details/14214705。这里仅仅做一些文字上的补充和贴一段示例代码。

二、主要内容

AVFormatContext是FFMPEG解封装(flv,mp4,rmvb,avi)功能的结构体,重要的变量如下:

struct AVInputFormat *iformat:输入数据的封装格式
AVIOContext *pb:输入数据的缓存
unsigned int nb_streams:视音频流的个数
AVStream **streams:视音频流
char filename[1024]:文件名
int64_t duration:时长(单位:微秒ms,转换为秒需要除以1000000)
int bit_rate:比特率(单位bps,转换为kbps需要除以1000)
AVDictionary *metadata:元数据

关于AVFormatContext的使用示例:

#include <stdio.h>//包含库extern "C"{#include "libavcodec/avcodec.h"#include "libavformat/avformat.h"#include "libswscale/swscale.h"#include "SDL/SDL.h"};int main(int argc, char* argv[]){//FFmpeg相关变量AVFormatContext*pFormatCtx;//AVFormatContext主要存储视音频封装格式中包含的信息char* filepath = "1.mp4";//文件路径av_register_all();//初始化libformat库和一些别的工作pFormatCtx = avformat_alloc_context();//分配formatcontext所需内存if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0){printf("Couldn't open input stream.\n");return -1;}printf("文件名: %s \n", pFormatCtx->filename);//文件名printf("流个数: %d \n", pFormatCtx->nb_streams);//视频流、音频流个数printf("时长: %d 秒\n", pFormatCtx->duration/1000000);//时长printf("总比特率: %d  kbps\n", pFormatCtx->bit_rate);//总比特率,这个时候是0,因为现在还获取不到。if (avformat_find_stream_info(pFormatCtx, NULL)<0){//读取流信息printf("Couldn't find stream information.\n");return -1;}printf("总比特率: %d kbps\n", pFormatCtx->bit_rate/1000);//总比特率,这个时候可以获取了。avformat_close_input(&pFormatCtx);//关闭输入return 0;}
运行结果:

        

左边是代码运行的结果,右边是1.MP4文件的详细信息。

0 0