ffmpeg获取本地视频详情

本文介绍了如何使用FFmpeg库读取并解析MP4文件,重点讲解了如何通过`avformat_open_input`和`avformat_find_stream_info`获取视频文件的流信息,并提取视频时长。适合初学者了解基本的音视频处理流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

流程示意图

程序代码

#include <iostream>

extern "C"
{
	#include <libavcodec/avcodec.h>
	#include <libavformat/avformat.h>
}
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")

using namespace std;

int main()
{
	//注册
	av_register_all();
	//上下文
	AVFormatContext* pFormat = NULL;
	const char* path = "aa.mp4";
	//打开本地文件
	int ret = avformat_open_input(&pFormat, path, NULL, NULL);
	if (ret < 0)
	{
		cout << "avformat_open_input error:" << ret;
		return -1;
	}
	//查找流信息
	ret = avformat_find_stream_info(pFormat, NULL);
	if (ret < 0)
	{
		cout << "avformat_find_stream_info error:" << ret;
		return -1;
	}
	int time = pFormat->duration;
	int second = time / 1000000;
	cout << "second=" << second << endl;
	//获取详细信息
	av_dump_format(pFormat, NULL, path, 0);

	system("pause");
	return 0;
}

运行结果:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值