FFMPEG结构体分析一:AVFormatContext

一、概述

       关于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文件的详细信息。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值