前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c/c++使用ffmepg库获取视频信息(宽高时长等)

c/c++使用ffmepg库获取视频信息(宽高时长等)

作者头像
大菊观
发布2023-10-16 15:45:25
2650
发布2023-10-16 15:45:25
举报

备忘一个使用ffmepg获取视频的宽高时长等信息的代码,是使用ffmepg的api不是通过exe命令行的方式。至于用的ffmepg库和头文件等,我传github和CSDN各一份。地址在最后。

下面是代码,首先:

代码语言:javascript
复制
//引入ffmpeg的头文件和lib,自己处理好库的路径
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
};

#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"avdevice.lib")
#pragma comment(lib,"avfilter.lib")
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"postproc.lib")
#pragma comment(lib,"swresample.lib")
#pragma comment(lib,"swscale.lib")

然后使用:

代码语言:javascript
复制
int _tmain(int argc, _TCHAR* argv[])
{
	//注册复用器,编码器等
	av_register_all();

	AVFormatContext *formatContext = NULL;

	// 打开视频文件
	if (avformat_open_input(&formatContext, "E:\\TestCode\\TestMfc\\big_buck_bunny.mp4", NULL, NULL) != 0) {
		printf("无法打开视频文件\n");
		return -1;
	}

	// 查找视频文件中的流信息
	if (avformat_find_stream_info(formatContext, NULL) < 0) {
		printf("无法查找视频流信息\n");
		return -1;
	}

	int videoStreamIndex = -1;
	AVCodecParameters *codecParameters = NULL;

	// 查找视频流
	for (int i = 0; i < formatContext->nb_streams; i++) {
		if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
			videoStreamIndex = i;
			codecParameters = formatContext->streams[i]->codecpar;
			break;
		}
	}

	if (videoStreamIndex == -1) {
		printf("无法查找视频流\n");
		return -1;
	}

	// 获取视频时长
	int64_t duration = formatContext->streams[videoStreamIndex]->duration;
	AVRational timeBase = formatContext->streams[videoStreamIndex]->time_base;
	int durationInSeconds = duration * timeBase.num / timeBase.den;

	int nWidth = codecParameters->width;
	int nHeight = codecParameters->height;

	printf("视频时长: %d 秒\n", durationInSeconds);

	// 关闭视频文件
	avformat_close_input(&formatContext);
	return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-05-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档