使用ffmpeg打开本地文件

其中 AVFormatContext 用来存储视音频封装格式(flv,mp4,rmvb,avi)中包含的所有信息 很多函数都要用到它作为参数。

avformat_open_input()该函数是打开多媒体数据并且获得一些相关的信息,其中填入的&pFormat是函数调用成功之后处理过的AVFormatContext结构体,path是打开的音视频流的url,   函数执行成功的话,其返回值大于等于0

avformat_find_stream_info () 该函数可以读取视音频流数据并且获得一些相关的信息,pFormat就是输入的AVFormatContext,函数执行成功的话,其返回值大于等于0;

#include <stdio.h>

extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>

}
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"avformat.lib")

int main()
{
	//测试DLL库是否成功
	printf("%s/n", avcodec_configuration());;


	AVFormatContext* pFormat = NULL;
    const char* path = "22.mp4";

	//打开视频文件
	int ret = avformat_open_input(&pFormat, path, NULL, NULL);

	//判断是否打开
	if (ret) {
		printf("打开失败\n");
		return -1;
	}
	printf("打开成功\n");

	//寻找流信息  H264 宽width  高height
	ret = avformat_find_stream_info(pFormat, NULL);

	//判断是否成功
	if (ret) {
		printf("失败\n");
		return -1;
	}
	printf("成功\n");

	//获取文件时长
	int time = pFormat->duration;
	printf("%d  时长",time);
	
	//以分秒显示
	int mbittime = (time / 1000000) / 60;
	int mmintime = (time / 1000000) % 60;
	printf("%d分/%d秒\n", mbittime, mmintime);

	return 0;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值